Wikunia
Wikunia

Reputation: 1592

JavaScript replace lowercase uppercase

I want to replace something (variable -> I call it "x" here) in a text (variable) with 'string'+x+'string'.

My code:

new_content = content.replace(new RegExp(x,"gi"),'string'+x+'string');

So I want to replace upper and lowercase x and the x in 'string'+x+'string'should be lowercase if the search xis lowercase as well. And the same thing for uppercase.

Is there a way like $1 for this situation?

Upvotes: 0

Views: 2193

Answers (1)

Guffa
Guffa

Reputation: 700312

You can use $& in the replacement string to insert the matched string:

new_content = content.replace(new RegExp(x,"gi"), 'string$&string');

Upvotes: 7

Related Questions