7

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.

0

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

-3

Just use a case-insensitive regex switch on the pattern that you wish to replace.

Something like:

myString.replace(/AnyCasE/gi, "anycase")

1 Comment

I don't know exact string to match. Sorry, maybe my question is not clear.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.