简体   繁体   English

Javascript用正则表达式全局替换

[英]Javascript replace globally with regex

I have special characters in my XML and i am using Javascript function to replace it. 我在XML中有特殊字符,并且正在使用Javascript函数来替换它。 I have found global search to replace in all my tags as. 我发现全局搜索将所有标签替换为。

 var res = outputText.replace(/&/g,"&"); 

But i also want to replace </Product> tag with <btn>Click NOW</btn></Product> 但我也想将</Product>标记替换为<btn>Click NOW</btn></Product>

How can i replace this i am trying like 我该如何替换我正在尝试的

res = res.replace(/</Product>/g,"<btn>Click  NOW</btn></Product>"); 

But it is giving me error invalid regular expression flag P 但这给了我错误invalid regular expression flag P

You need to escape the / with \\ : 您需要使用\\转义/

res = res.replace(/<\/Product>/g,"<btn>Click  NOW</btn></Product>"); 
//------------------^

您必须先在/之前加上/

/<\/P

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

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