Sometimes, we want to remove parenthesis from a string in JavaScript.
In this article, we’ll look at how to remove parenthesis from a string in JavaScript.
How to remove parenthesis from a string in JavaScript?
To remove parenthesis from a string in JavaScript, we can call the JavaScript string’s replace with a regex that matches all parentheses, brackets, and braces and replace them with empty strings.
For instance, we write:
const str = '[]({foobar})'
const newStr = str.replace(/[])}[{(]/g, '');
console.log(newStr)
We call str.replace with /[])}[{(]/g to match all parentheses, brackets, and braces in str and replace them all with empty strings.
Then we assign the returned string to newStr.
As a result, newStr is 'foobar'.
Conclusion
To remove parenthesis from a string in JavaScript, we can call the JavaScript string’s replace with a regex that matches all parentheses, brackets, and braces and replace them with empty strings.