Regex - convert the string to camel caps by `dot` - javascript

I am looking for a solution to convert my string to camelcaps by the dot it contains.
here is my string: 'sender.state'
I am expecting the result as : 'senderState';
I tried this: 'sender.state'.replace(/\./g, ''); it removes the . but how to handle the camel caps stuff?

You can pass a function to .replace():
'sender.state'.replace(/\.([a-z])/g, (match, letter) => letter.toUpperCase());

Without regex - this might help :
var str = "bad.unicron";
var temp = str.split(".");
return temp[0]+temp[1].charAt(0).toUpperCase() + temp[1].slice(1);
I'll try to come up with regex

Related

How Can I Use Regex To Split A String on Letters and Numbers and Carets followed by digits

So I was wondering how I would go about splitting a String on specific characters/conditions using Regular Expressions like the following:
On digits
On Letters
On digits following a caret
Here could be an example:
var str1 = "62y^2";
Would return as an array:
[62,y,^2]
Thanks for the help.
You can use String.match() instead of split with the following regular expression (Regex101):
var str="62y^2ad23^123";
var result = str.match(/\^?\d+|[a-z]+/gi);
console.log(result);
You may try the following approach:
var str="62y^2ad23^123";
console.log(str.split(/(\^\d+|[a-zA-Z]+|\d+)/).filter(function(n){ return n != "" }));
Try the below regex
var str = "62y^2";
str.match(/\^(\d+|[a-zA-Z]+)|[a-zA-Z]+|\d+/g); // ["62", "y", "^2"]

Javascript: Manipulate string to remove underscore and capitalize letter after

Lets say I am receiving a string like so:
var string = "example_string"
var otherString = "example_string_two"
And I want to manipulate it to output like this:
string = "exampleString"
otherString = "ExampleStringTwo"
Basically, I want to find any underscore characters in a string and remove them. If there is a letter after the underscore, then it should be capitalized.
Is there a fast way to do this in regex?
You could look for the start of the string or underscore and replace the found part with an uppercase character.
var string= 'example_string_two';
console.log(string.replace(/(^|_)./g, s => s.slice(-1).toUpperCase()));
A regular expression like /_([a-zA-Z])/g will do with a proper callback function in String.prototype.replace. See snippet below.
function camelize (dasherizedStr) {
return dasherizedStr
.replace(/_([a-zA-Z])/g, function (m1, m2) {
return m2.toUpperCase()
});
}
console.log('example_string_foo:', camelize('example_string_foo'));
console.log('foo_Bar:', camelize('foo_Bar'));
Yeah you could use regex methods and simply replace the underscore and i'll give you an example :
var string = "example_string"
string.replace('_','');
But you could also do this in classic JS, which is pretty fast in on it's self
Example:
var string = "example_string"
string.split('_').join('');
If you are looking for something more, please comment below.
You can easily replace using JavaScript
var string= 'example_string_two';
console.log(string.replaceAll('_', ' '))
Output : 'example string two'
you can replace any word, underscore, dashes using javascript
here is code
var str= 'example_string_two';
console.log(var newStr = str.replace("_", " "));
output:
examplestringtwo

Replace expression between two punctuation letters in javascript

I have a string like the following,
var str = "abcd-12ad3dgs4g56.com"
I want like the following from this
abcd.com
I have to replace only -*. expression with ..
How do I do this in JavaScript?
Simply try this
str.replace( /-\w+/, "" ); //"abcd.com"
var str = "abcd-12ad3dgs4g56.com"
console.log(str.replace(/-\w+/, ""));
You could use a regular expression with a positive lookahead.
var str = "abcd-12ad3dgs4g56.com";
console.log(str.replace(/-.*(?=\.)/g, ''));

String : Replace function with expression in Javascript

A string representing a currency is to be converted to a number.
For example:
Input : "125.632.454.454.403,51"
Output expected : 125632454454403.51
Currently I am trying:
Trial 1)
a = "125.632.454.454.403,51";
a.replace(/./, '');
Result = "25.632.454.454.403,51"
Trial 2)
a = "125.632.454.454.403,51";
a.replace(/./g, '');
Result = ""
But I expect the replace function to find all the occurrences of "." and replace by "".
Trial 3)
a = "125.632.454.454.403,51";
a.replace(/,/, '');
Result = "125.632.454.454.40351"
I would be glad if I find a fix for this.
You need to use \. instead of .. The dot (.) matches a single character, without caring what that character is. Also you can do it with single replace() with callback .
var str = "125.632.454.454.403,51";
str = str.replace(/\.|,/g, function(m) {
return m == '.' ? '' : '.'
});
document.write(str);
try:
var str = "125.632.454.454.403,51" ;
var result = str.replace(/\./g,'').replace(/\,/g,'.');
console.log(Number(result))
replace returns the changed string, it does not change it in-place!
You can find this out, by refering to the documentation.
Use
var Result = a.split('.').join("");
console.log(Result);
. has specific meaning in a regex. It matches any character. You need to escape the dot if you are actually looking for the character itself
var a = "125.632.454.454.403,51";
var result = a.replace(/\./g,"");
You can also do (parseFloat(a.replace(/[^0-9]+/g,""))/100)
And if you have to do this for multiple currencies, I would recommend looking into autonumeric.js. It handles all this for you.

How to trim string between slash?

I need to take vale between / slashes. for example ./ankits/ankitt$. Here I need to take the ‘antits’ string. Do i need to use reg-expression or it can be done using trim method?
Please help me to solve this
What about .split method?
var str = './ankits/ankitt$.';
var arr = str.split('/');
console.log(arr);
It will split the string in array, forward slash will be used as separator.
This regular expression will return what's inbetween the slashes:
/\/(.*?)\//
Test code:
var regex = /\/(.*?)\//;
var str = './ankits/ankitt$.';
var result;
result = regex.exec(str);
alert(result[0]);
Short form:
"./ankits/ankitt$".match(/\/(.*?)\//)[1]
Try this
"./ankits/ankitt$".match(/\/([^\/]+)\//)[1]

Categories

Resources