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.
Example:
// Original string const str = "This (is) a ran(dom (string)."; // Remove '(' or ')' from string const strWithoutParenthesis = str.split('(').join('') .split(')').join(''); console.log(strWithoutParenthesis); // 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('('); console.log(subArr); // 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(''); console.log(strWithoutLeftParenthesis); // 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(')'); console.log(subArrNew); // 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(''); console.log(result); // 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, ''); console.log(newStr); // 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.