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
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
An efficient solution is to let
.*
eat up everything before the last(
.*\(
matches any amount of any character until the last literal(
([^)]+)
captures one or more characters that are not)
[1]
grab captures of group 1 (first capturing group).[\s\S]
instead of.
dot for multiline strings.Here is a demo at regex101
If the last closing bracket is always at the end of the sentence, you can use Jonathans answer. Otherwise something like this might work:
Try this
See it here on regexr
The part inside the brackets is stored in capture group 1, therefor you need to use
match()[1]
to access the result.The lookahead,
(?=[^()]*$)
, asserts that there are no more parentheses before the end of the input.