srini
srini

Reputation: 6969

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

Upvotes: 1

Views: 634

Answers (3)

sinsedrix
sinsedrix

Reputation: 4775

Simply :

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

or even

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

Upvotes: 0

anubhava
anubhava

Reputation: 785146

Something like this should work:

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

Upvotes: 0

Mariusz Jamro
Mariusz Jamro

Reputation: 31653

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

Upvotes: 5

Related Questions