简体   繁体   中英

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" )';

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)); 

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