Replace multi string in Javascript

There is only way is replacing the string by regular expression. For example:

s = s.replace(/yourPaternString/g, 'yourNewValue');
However, the patern string may not work when it contains metacharacter of regex, such as . $ ^ { [ ( | ) * + ? \. We need to escape these characters by following code:
patern = patern.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
text = text.replace(new RegExp(patern, 'g'), 'yourNewValue');

Bookmark the permalink. RSS feed for this post.

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.