Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change case using Javascript regex

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.

like image 426
DixonD Avatar asked Oct 08 '10 12:10

DixonD


1 Answers

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.

like image 123
Nick Craver Avatar answered Sep 29 '22 01:09

Nick Craver