Categories
JavaScript Answers

How to remove parenthesis from a string in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *