EN
JavaScript - RegEx to match text between parentheses
1 answers
0 points
I have the following string:
xxxxxxxxxx
1
var text = 'some_text/([0-9])/([a-z])';
and I want to use a regular expression to match everything between the parentheses and get an array of matches like the one below:
xxxxxxxxxx
1
[[0-9], [a-z]]
1 answer
0 points
Use the following regex: /\(([^()]+)\)/g
.
Practical example:
xxxxxxxxxx
1
var text = 'text/([0-9])/([a-z])';
2
var regex = /\(([^()]+)\)/g;
3
4
var result = text.match(regex);
5
6
if (result) {
7
console.log(result[0]); // ([0-9])
8
console.log(result[1]); // ([a-z])
9
}
You can also remove the wrapping parentheses using:
xxxxxxxxxx
1
console.log(result[0].substring(1, result[0].length - 1)); // [0-9]
2
console.log(result[1].substring(1, result[1].length - 1)); // [a-z]
0 commentsShow commentsAdd comment