简体   繁体   English

Javascript .replace()替换所有出现的/

[英]Javascript .replace() replace all occurrences of /

如果我有一个包含</custom-tag>的字符串,我如何使用replace来查找字符串中所有出现的标记,并将其替换为“”,例如mystr.replace(/</constant>/g,"")不起作用。

You need to escape the / so that it isn't interpreted as the end of the regex. 你需要转义/以便它不被解释为正则表达式的结束。

mystr.replace(/<\/constant>/g, "")

Of course, if your search is a constant expression, as it is here, you can use the following technique to perform a global replace without regular expressions: 当然,如果您的搜索是一个常量表达式,就像在这里一样,您可以使用以下技术在没有正则表达式的情况下执行全局替换:

mystr.split("</constant>").join("")
mystr.replace(/<\/constant>/g,"");

这应该做到这一点

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

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