Gracie williams
Gracie williams

Reputation: 1145

Match multiple result using regex

Hi I want to match multiple results via regex in javascript , I tried below code.I want to match only numbers thats next to options.

/Option\("(\d+)"/g.exec(string)

Above result returns only 1 result, I searched in stackoverflow , they told to use match , so i tried below

string.match(/Option\("(\d+)"/g);

Above is returning multiple result but with full string , I just want all the available (\d+) parts.

Edit : input string is below

var o = 'new Option("26500","26,500.00" ); var o = new Option("26700","26,700.00" ); var o = new Option("26800","26,800.00" )';

Upvotes: 0

Views: 72

Answers (1)

Nick
Nick

Reputation: 147166

You can use a positive lookahead for the characters which come after the number you want to capture to get all the results using String.match:

(\d+)(?="\s*,\s*")

If you have a version of JavaScript that supports lookbehinds you can also use a positive lookbehind:

(?<=Option\(")(\d+)

Demo of lookbehind on regex101.com

const str = 'new Option("26500", "26,500.00" ); var o = new Option("26700" ,"26,700.00" ); var o = new Option("26800","26,800.00" )';
const re = /(\d+)(?="\s*,\s*")/g;
console.log(str.match(re));

Upvotes: 2

Related Questions