I am parsing a file in javascript & have to capture some values. For example velocity values as below. Eg:
> "velocity(22)".match("\\(([^()]*)\\)");
[ '(22)', '22', index: 8, input: 'velocity(22)' ]
Above works fine and 22 gets captured in match[1]. Problem is when the values have units mentioned in parantheses. Eg:
> "velocity(22 (m/s))".match("\\(([^()]*)\\)");
[ '(m/s)', 'm/s', index: 12, input: 'velocity(22 (m/s))' ]
As seen above, m/s is captured instead of 22 (m/s)
What is the proper way to ignore parentheses when capturing within parentheses?
/\((\d+)(?:\s*\([^()]*\))?\)/RegExpfrom the string.console.log('velocity(22 (m/s))'.match(/\(((?:[^)(]+|\([^)(]*\))*)\)/)[1]);