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?
We call str. replace with /[\])}[{(]/g to match all parentheses, brackets, and braces in str and replace them all with empty strings.
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.
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.
Try the following:
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(|\)/g, "");
A little bit of REGEX to grab those pesky parentheses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With