简体   繁体   English

JavaScript替换小写字母

[英]JavaScript replace lowercase uppercase

I want to replace something (variable -> I call it "x" here) in a text (variable) with 'string'+x+'string'. 我想用'string'+ x +'string'替换文本(变量)中的某些内容(变量->在这里我称其为“ x”)。

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. 因此,我想替换大写和小写的x,并且如果搜索x也是小写的,则'string'+x+'string'x也应小写。 And the same thing for uppercase. 与大写相同。

Is there a way like $1 for this situation? 在这种情况下是否有像$1这样的方法?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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