简体   繁体   中英

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 x is lowercase as well. And the same thing for uppercase.

Is there a way like $1 for this situation?

您可以在替换字符串中使用$&插入匹配的字符串:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM