Home >Web Front-end >JS Tutorial >How to Extract Strings within Parentheses using Regular Expressions in JavaScript?
Extracting strings enclosed within parentheses from a larger string is a common task in text processing. So, let's explore a solution to this problem using regular expressions in JavaScript.
To capture the string between parentheses, we can use the following regular expression:
/\(([^)]+)\)/
Let's break down this expression:
To use this expression, we can follow these steps:
For example, let's consider the string:
I expect five hundred dollars (0).
When we apply our regular expression, the matched string will be:
0
Here's how you can implement this in JavaScript:
var regExp = /\(([^)]+)\)/; var matches = regExp.exec("I expect five hundred dollars (0)."); // matches[1] contains the value between the parentheses console.log(matches[1]); // Prints "0"
The above is the detailed content of How to Extract Strings within Parentheses using Regular Expressions in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!