JavaScript replace lowercase uppercase letters

I want to replace something (variable → I call it "x" here) in the text (variable) with "string" + x + "string".

My code is:

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

So, I want to replace the uppercase and lowercase x, and x in 'string'+x+'string'should be lowercase if the search is xalso lowercase. And the same is for capital letters.

Is there a way similar $1for this situation?

0
source share
1 answer

You can use $&in a replacement string to insert a consistent string:

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

All Articles