Panagiotis Koursaris Panagiotis Koursaris - 2 months ago 8
Javascript Question

Get the next 6 any numbers after specific two numbers

I would like to get from string the numbers 24 or 99 with the next six any numbers. For example, imagine the followed string:

anytext 24 824 750 anytext 99 659 440 anytext 24 234 423 24743534 anytext


What I what to get is:

24824750 99659440 24234423 24743534

Answer
var r=/(24|99)(\s*[0-9]){6}/g;
var s='anytext 24 824 750 anytext 99 659 440 anytext 24 234 423 24743534 anytext';
var m;
while(true) {
    m = r.exec(s);
    if(!m) break;
    console.log(m[0].replace(/\s/g,''));
}

You can change \s to space if that's what you want.

Comments