Javascript: Remove Parenthesis from String

This article will remove all brackets from a javascript string using different methods and example illustrations. While developing in any language, removing the parenthesis from a string is common before processing it. We will also discuss removing square brackets and other brackets in this article.

Table of Contents:-

Javascript String Remove Parenthesis using replace()

Javascript’s replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

syntax:

replace(regExp, newSubstring)

Here, regExp is the regular expression object to be used. These objects are patterns used to match character combinations in strings. The argument newSubstring is the string within that will replace the pattern.

Advertisements

Example:-

Remove all brackets from the string “(javascript {{is} popular) “

Code:-

var dummyString = "(javascript {{is} popular) ";
let stringWithoutBrackets = dummyString.replace(/[{()}]/g, '');
console.log("Original String: " + dummyString);
console.log("Final String: " + stringWithoutBrackets);

Output:-

Original String: (javascript {{is} popular) 
Final String: javascript is popular 

Explanation

Here in the above code, we are using replace() method with RegExp. The regular expression is the first argument that this replace() method should use to replace the pattern found in the string. The second argument is the replacement string, nothing (”) in our case.

  • Regular Expression Reference:
  • / and / marks the beginning and end of the string.
  • [ and ] specifies the character group; that matches any character in the character group.
  • {()} specifies to match the brackets { ,(, } and ) in our case.
  • g specifies the global search to match all the occurrences within the string.

Javascript String Remove Parenthesis using replaceAll()

Javascript’s replaceAll() method will replace all the pattern matches within a string with a replacement. The first argument is a pattern which can be a string or a RegExp. This first argument is to be searched in string and replaced with a replacement. The second argument is a replacement which can be a string or a function.

Example:-

Remove all brackets from the string “(javascript[[]] {{is} popular)”

Code:-

var dummyString = "(javascript[[]] {{is} popular) ";
let stringWithoutBrackets = dummyString.replaceAll(/[{()}[\]]/g, '');
console.log("Original String: " + dummyString);
console.log("Final String: " + stringWithoutBrackets);

Output:-

Original String: (javascript[[]] {{is} popular) 
Final String: javascript is popular 

Explanation

Here the above code uses the replaceAll() method to replace all the occurrences of the provided brackets in the regular expression. The Regular /[{()}[\]]/g expression is passed as the first argument, and a replacement is passed as the second argument, nothing (”) in our case. 

  • Regular Expression Reference:
  • / and / marks the beginning and end of the string.
  • [ and ] mark the character group that matches any character in the character group.
  • {()}[\] specifies to match the brackets { , }, ( , ), [ and ]
  • g specifies the global search to match all the occurrences within the string.

Note: The square brackets will also be removed from the string using the above code.

How to Remove Square Brackets from JavaScript String

Example:-

Remove all brackets from the string “(javascript[[]] {{is} popular)”

Code:-

var dummyString = "(javascript[[]] {{is} popular) ";
let stringWithoutBrackets = dummyString.replace(/[{()}[\]]/g, '');
console.log("Original String: " + dummyString);
console.log("Final String: " + stringWithoutBrackets);

Output:-

Original String: (javascript[[]] {{is} popular) 
Final String: javascript is popular 

Explanation:-

Here we have included the [\] within the regular expression of the replace method. Since the outer [ and ] in the regular expression define the character group. Adding square brackets within them gets neglected. Hence to find the square brackets along with another parenthesis, we need to define them this way {()}[\].

Javascript String Remove Parenthesis using using split() and join()

The split() method in javascript returns an array of substrings formed by splitting a given string.

The join() method in javascript joins the array elements back into a string.

Example:-

Remove all brackets from the string “(java[script {{is} ]]popular)”

Code:-

var dummyString = "(java[script {{is} ]]popular) ";
let stringWithoutBrackets = dummyString.split('{').join('').split('}').join('').
                            split('[').join('').split(']').join('').
                            split('(').join('').split(')').join('');

console.log("Original String: " + dummyString);
console.log("Final String: " + stringWithoutBrackets);

Output:-

Original String: (java[script {{is} ]]popular) 
Final String: javascript is popular 

Explanation:-

The above code shows that we split the string based on multiple characters that are brackets of all kinds (,{,(,],[,) using the split() method subsequently into an array of substrings. Then join the array back into one string using the join() method.

I hope this article helped you delete all brackets from a string in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top