Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for getting text between the last brackets ()

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

For example

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

like image 904
user1376366 Avatar asked May 05 '12 06:05

user1376366


People also ask

How do I extract text between brackets in Python?

Method 1: Slicing and str. The simplest way to extract the string between two parentheses is to use slicing and string. find() .

How do you match brackets in regex?

Use %b() ( %b{} / %b[] for curly braces / square brackets): for s in string.


1 Answers

Try this

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

See it here on 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, therefor you need to use match()[1] to access the result.

like image 153
stema Avatar answered Oct 06 '22 01:10

stema