How to change case of some backreference in String.replace()
? I want to match some part in text and change its case to upper/lower.
You can use a regex for your match then pass a function, here's an example for converting CSS properties:
"margin-top".replace(/-([a-z])/, function(a, l) { return l.toUpperCase(); })
//result = "marginTop"
You can test it out here. This regex takes any -alpha
(one character) and turns it into -upperalpha
, it's just an example though, any regex works, and you'll want to call .toUpperCase()
or .toLowerCase()
(or anything else really) on the second argument in the callback, which is the current match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With