简体   繁体   中英

Regular expression to get numbers from the string in javascript

I have a String like the following

 minmaxSize2-8
 minmaxSize12-20

How to get the range from the above strings.I need to get 2-8 and 12-20. Please suggest the regular expressions in javascript

You can do it like this:

var myString = "minmaxSize12-20";
var myRegexp = /(\d+)-(\d+)/g; // Numbers dash Numbers
var match = myRegexp.exec(myString);
alert(match[1]);  // 12
alert(match[2]);  // 20

Something like this should work:

var str = 'minmaxSize12-20';
var range = str.replace(/^.*?Size/i, ''); // returns 12-20

Simply :

"minmaxSize12-20".match(/(\d+)-(\d+)/)

or even

/(\d+)-(\d+)/.exec("minmaxSize12-20");

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