jQuery - Replace all parentheses in a string

HaBo picture HaBo · Feb 2, 2012 · Viewed 56.2k times · Source

I tried this:

mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace("(", "").replace(")", "");

It works for all double and single quotes but for parentheses, this only replaces the first parenthesis in the string.

How can I make it work to replace all parentheses in the string using JavaScript? Or replace all special characters in a string?

Answer

George Reith picture George Reith · Feb 2, 2012

Try the following:

mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(|\)/g, "");

A little bit of REGEX to grab those pesky parentheses.