Retrieve only part of the matched string using regex in Javascript

I have a string like

foo (123) bar 

I want to get all numbers surrounded by delimiters ( and ) .

If I use varname.match(/\([0-9]+\)/) , my delimiters are included in the response, and I get "(123)" when I really want "123".

How can I get only part of the matched string without following it with varname.replace() ?

+4
source share
1 answer

Yes, use capture (unshielded) parsers:

 varname.match(/\(([0-9]+)\)/)[1] 
+7
source

Source: https://habr.com/ru/post/1277344/


All Articles