Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript .replace() replace all occurrences of /

If I have a string that contains </custom-tag> , how can I use replace to find all occurrences of this tag in a string and replace it with "" , for example mystr.replace(/</constant>/g,"") will not work.

like image 410
ProllyGeek Avatar asked Dec 16 '22 02:12

ProllyGeek


1 Answers

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("")
like image 97
Alexis King Avatar answered Dec 26 '22 01:12

Alexis King