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.
views:
34answers:
2
+1
A:
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.
Nick Craver
2010-10-08 12:14:49
Thanks, it helped.
DixonD
2010-10-08 13:45:44
A:
Just use a case-insensitive regex switch on the pattern that you wish to replace.
Something like:
myString.replace(/AnyCasE/gi, "anycase")
Assaf Lavie
2010-10-08 12:17:29
I don't know exact string to match. Sorry, maybe my question is not clear.
DixonD
2010-10-08 13:45:10