Reputation:
This:
replace(/([a-zA-Z])([A-Z]+)/g,'$1'+'$2'.toLowerCase() )
Does not convert $2 to lowercase (unchanged)
But :
replace(/([a-zA-Z])([A-Z]+)/g,'$1'+'heLLo'.toLowerCase() )
Makes every upper case being replaced by "hello" (as expected)
Why?
Thanks.
Upvotes: 0
Views: 171
Reputation: 3404
Because toLowerCase
is executed before it gets in the main function replace
. This is what you are doing:
replace(/([a-zA-Z])([A-Z]+)/g,'$1hello')
You can do what you want with this:
var replaceLogic = function(fullMatch, firstGroup, secondGroup){
return firstGroup.toLowerCase() + secondGroup;
};
string.replace(/([a-zA-Z])([A-Z]+)/g, replaceLogic);
Upvotes: 2