Regular expression to get text between last brackets ()

I want to extract text between the last ()using javascript

for instance

var someText="don't extract(value_a) but extract(value_b)";
alert(someText.match(regex));

The result should be

value_b

thanks for the help

+5
source share
4 answers

try it

\(([^)]*)\)[^(]*$

See here at regexr

var someText="don't extract(value_a) but extract(value_b)";
alert(someText.match(/\(([^)]*)\)[^(]*$/)[1]);

The part inside the brackets is stored in capture group 1, so you need to use it match()[1]to access the result.

+8
source
/\([^()]+\)(?=[^()]*$)/

Appearance (?=[^()]*$)states that there are more parentheses before the end of input.

+5
source

.* (

var str = "don't extract(value_a) but extract(value_b)";

var res = str.match(/.*\(([^)]+)\)/)[1];

console.log(res);
Hide result

Here is the demon in regex101

+2
source

If the last closing bracket is always at the end of the sentence, you can use Jonathans answer. Otherwise, this might work:

/\((\w+)\)(?:(?!\(\w+\)).)*$/
0
source

All Articles