在特定的两个数字之后得到接下来的6个任意数字

Get the next 6 any numbers after specific two numbers

本文关键字:数字 之后 接下来 任意 6个 两个      更新时间:2023-09-26

我想从字符串中得到数字24或99与接下来的六个任意数字。例如,想象下面的字符串:

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

我想要得到的是:

24824750 99659440 24234423 24743534
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,''));
}

你可以把's改成空格

另一种方法(ES6代码):

var txt = 'anytext 24 824 750 anytext 99 659 440 anytext 24 234 423 24743534 anytext';
var res = txt.match(/(24|99)('s*'d){6}/g).map( m => m.replace(/'s+/g, '') );
console.log(res);

您可以这样做

var str = "anytext 24 824 750 anytext 99 659 440 anytext 24 234 423 24743534 anytext",
 result = str.replace(/'s+/g,"")
             .match(/(?:24|99)'d{6}/g);
console.log(result);