HaBo HaBo - 21 days ago 7
Javascript Question

jQuery - Replace all parentheses in a string

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

Try the following:

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

A little bit of REGEX to grab those pesky parentheses.

Comments