Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 374
HaBo Avatar asked Feb 02 '12 15:02

HaBo


People also ask

How do you change all parentheses?

We call str. replace with /[\])}[{(]/g to match all parentheses, brackets, and braces in str and replace them all with empty strings.

How do I remove parentheses from a string?

To remove parentheses from string using Python, the easiest way is to use the Python sub() function from the re module. If your parentheses are on the beginning and end of your string, you can also use the strip() function.

How to remove parentheses from array in JavaScript?

In JavaScript, to remove parentheses from a string the easiest way is to use the JavaScript String replace() method. Since parentheses are special characters we must escape them by putting a backslash in front of them, as seen in the code above.


1 Answers

Try the following:

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

A little bit of REGEX to grab those pesky parentheses.

like image 103
George Reith Avatar answered Oct 07 '22 15:10

George Reith