Lets say I am receiving a string like so:
JavaScript
x
3
1
var string = "example_string"
2
var otherString = "example_string_two"
3
And I want to manipulate it to output like this:
JavaScript
1
3
1
string = "exampleString"
2
otherString = "ExampleStringTwo"
3
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?
Advertisement
Answer
You could look for the start of the string or underscore and replace the found part with an uppercase character.
JavaScript
1
3
1
var string= 'example_string_two';
2
3
console.log(string.replace(/(^|_)./g, s => s.slice(-1).toUpperCase()));