简体   繁体   中英

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,"");

这应该做到这一点

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