How to Remove Parenthesis and Content in JavaScript

Learn how to use regular expressions to easily remove text between parenthesis in JavaScript.

How to Remove Parenthesis and Content in Javascript

Manipulating text in Javascript can become complicated, especially if you dip your toes into the world of regular expressions. Luckily we’re here to help you make sense of it all. The code below will allow you to remove the text between parenthesis and other encapsulating characters such as curly and square brackets. Let’s dive into the code.

How to Remove Parenthesis and Content in JavaScript

To remove text between parenthesis in a Javascript string, we will use a regular expression with the replace() function. Let’s take a look at a coding example:

let input = "Hello from Techozu (remove me)"
let filtered = input.replace(/\s*\(.*?\)\s*/g, '')

console.log(filtered)

// OUTPUT
// "Hello from Techozu"
      
    

The replace() string prototype in Javascript takes two variables. The first variable is either a regular expression or a normal string. The second variable is your new text. In our case, we will use a regular expression that finds a set of parenthesis. It targets the parenthesis themselves, the text in between, and the whitespace around it. We replace our regex target with a blank string, removing it.

replace() can also be used to replace the first occurrence of a regular string

If you would like to avoid removing the whitespace around the parenthesis, you can use this code:

let input = "Hello from Techozu (remove me)"
let filtered = input.replace(/\(.*?\)/g, '')

console.log(filtered)

// OUTPUT
// "Hello from Techozu "
      
    

If you also want to include curly brackets when removing the content, you can use the following code:

let input = "Hello from Techozu (remove me) {me too}"
let filtered = input.replace(/\s*[{(].*?[)}]\s*/g, '')

console.log(filtered)

// OUTPUT
// "Hello from Techozu"
      
    

In this example, we add two character classes [{(] and [)}] to catch both the curly brackets {} and the regular parenthesis (). To also catch square brackets, you can use the code below:

let input = "Hello from Techozu (remove me) {me too} [dont forget me]"
let filtered = input.replace(/\s*[\[{(].*?[)}\]]\s*/g, '')

console.log(filtered)

// OUTPUT
// "Hello from Techozu"
      
    

Remember that you can chain replace() functions. Sometimes this is a better idea for readability than running a single complicated regex.

How to Remove Parenthesis without Text in JavaScript

If you want to remove just the parenthesis but leave the text, you can use the following code:

let input = "Hello from (Techozu)"
let filtered = input.replace(/[{()}]/g, '')

console.log(filtered)

// OUTPUT
// "Hello from Techozu"
      
    

In this regex, we create a character class that catches both the curly brackets and parenthesis (){}.

The /[{()}]/g regex will match any character in the character class. It doesn’t specifically look for pairs. If there is a single parenthesis in your input string, it will be removed. 

Now you know how to remove parenthesis and content in Javascript. You can code up a very robust solution with the extra code snippets to remove other encapsulating characters.

Check out our Javascript and NodeJS section for more useful Javascript coding tips.