JavaScript replace lowercase uppercase

橙三吉。 提交于 2020-01-03 05:19:51

问题


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?


回答1:


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

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


来源:https://stackoverflow.com/questions/19252139/javascript-replace-lowercase-uppercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!