4

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()?

1 Answer 1

8

Yes, use capturing (non-escaped) parens:

varname.match(/\(([0-9]+)\)/)[1]
Sign up to request clarification or add additional context in comments.

1 Comment

The '[1]' is the part the picks the first substring match. If it was '[0]' instead, it would return the entire match (e.g. '(123)')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.