user1937021
user1937021

Reputation: 10801

Regex to retrieve numbers from string

I have this string:

var string = "look1_slide2";

I would like to extract the the look number ie 1 and the slide ie 2 and save them in two different variables, I guess I could do it with a Regex but not sure how. Any help? The string will always have that format btw

Thanks!

Upvotes: 0

Views: 119

Answers (2)

SiCK
SiCK

Reputation: 367

Since your string is always in that format you can simply read second and third entries of the returned regex matches array :

var string = 'look1_slide2';
var regex = /look(\d)_slide(\d)/g;

matches = regex.exec(string);
console.log(matches[1]);
console.log(matches[2]);

See RegExp.exec() doc

Upvotes: 1

I-Kod
I-Kod

Reputation: 189

var txt = "#div-name-1234-characteristic:561613213213";
var numb = txt.match(/\d/g);
numb = numb.join("");
alert (numb);​

This will print 1234561613213213

Upvotes: 0

Related Questions