How to Remove Parentheses from a String in JavaScript?

To remove all the occurrences of parentheses(either '(' or ')' ) from a string in JavaScript, you can call the split() and join() methods on the specified string in a combination.

The String.split() method splits a string into an array of substrings based on a specified delimiter.

On the other hand, the Array.join() method joins the elements of the array into a string using a specified separator between each element.


// Original string
const str = "This (is) a ran(dom (string).";

// Remove '(' or ')' from string
const strWithoutParenthesis = str.split('(').join('')
// Output: 👉  "This is a random string."

But how does this code works?

Well, let me break this down into 4 different steps.

Step 1: In the very first step, we split the given string at '(' character using the String.split('(') method. This results in an array of substrings split at '(' character:

// Original string
const str = "This (is) a ran(dom (string).";

// Split string at '('
const subArr = str.split('(');

// Output: 👉 ['This ', 'is) a ran', 'dom ', 'string).']

Step 2: In the second step, we join back each element of the array into a string using the Array.join('') method. This results in a string without any opening parentheses('(').

// Array of substrings from step 1
const subArr = ['This ', 'is) a ran', 'dom ', 'string).']

// Join each element of array with empty string ''
const strWithoutLeftParenthesis = subArr.join('');

// Output: 👉 "This is) a random string)."

Step 3: To remove the remaining ')' character from the string, we again split it at the ')' character using the String.split(')') method.

This again results in an array of substrings:

// String from step 2
const strWithoutLeftParenthesis = "This is) a random string).";

// Split the string at each ')' character
const subArrNew = strWithoutLeftParenthesis.split(')');

// Output: 👉 ['This is', ' a random string', '.']

Final step: Finally, we join back each substring of the array with an empty string '' using the Array.join() method. This results in the final string that doesn’t contain any parenthesis.

See the final result:

// Array from step 3
const subArrNew = ['This is', ' a random string', '.'];

// Join each substring with an emtpy string ''
const result = subArrNew.join('');

// Output: 👉 "This is a random string."

Let’s next discuss one more approach to remove all the parenthesis from a string.

Method 2: Remove Parentheses from a String using Regular Expression

You can remove all occurrences of parentheses from a string using regular expression.

A regular expression is a sequence of characters that define a search pattern, used to match characters or substrings in a given string.

To remove all parentheses from a string using a regular expression in JavaScript, you can use the replace() method. The replace() method searches a string for a specified pattern and replaces the matches with a replacement string.

The following example removes all parentheses from a string using a regular expression :

// Original String
const str = "This (is) a (test) string.";

// Replace parentheses with empty string ''
const  newStr = str.replace(/[()]/g, '');

// Output: 👉 "This is a test string."

In this example, the replace() method is called on the string str, with a regular expression /[()]/g as the pattern to search for.

The regular expression /[()]/g matches any occurrence of either ( or ) in the string, using the square brackets to specify a character set. The g flag indicates that the regular expression should be applied globally to the string, matching all occurrences of the pattern.

The second argument to the replace() method is an empty string '', which serves as the replacement string. This effectively removes all occurrences of ( and ) from the original string.

That’s all for the day. Happy Coding.


  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts