简体   繁体   中英

Retrieve only a portion of a matched string using regex in Javascript

I've got a string like

foo (123) bar

I want to retrieve all numbers surrounded with the delimiters ( and ) .

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

How can I retrieve only a portion of the matched string without following it up with varname.replace() ?

是的,使用捕获(非转义)的parens:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM