Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript RegExp to CamelCase a hyphened CSS property

I am trying to change CSS properties like this one.

-moz-border-radius

To the JavaScript CSS property like so.

MozBorderRadius

I am using this RegExp.

var exp = new RegExp('-([a-z])', 'gi');
console.log('-moz-border-radius'.replace(exp, '$1'));

All I need to do is convert $1 to upper case so it can cammelcaseify (yes I made that word up...) my CSS property into a JavaScript based one. Is this possible?

Thanks.

like image 606
Olical Avatar asked Feb 11 '11 13:02

Olical


1 Answers

You would be better off using a function as the second parameter in replace(), and you could also use a regex literal instead of the RegExp constructor:

var replaced = '-moz-border-radius'.replace(/-([a-z])/gi, function(s, group1) {
    return group1.toUpperCase();
});
like image 88
Tim Down Avatar answered Oct 02 '22 13:10

Tim Down