Monday, 13 March 2017

Regex to capture comma separated numbers enclosed in quotes

I have a large set of JavaScript snippets each containing a line like:

function('some string without numbers', '123,71')

and I'm hoping to get a regex together to pull the numbers from the second argument. The second argument can contain an arbitrary number of comma separated numbers (inlcuding zero numbers), so the following are all valid:

''
'2'
'17,888'
'55,1,6000'
...

The regex '(?:\d+|,)*' successfully matches the quoted numbers, but I have no idea how to match each of the numbers. Placing a capture group around the \d+ seems to capture the last number (if there is one present -- it doesn't work if the second argument is just ''), but none of the others.



via Benjamin Tillman

No comments:

Post a Comment