2016-09-16 1 views
0

次の6つの数字で数字24または99を文字列から取得したいと考えています。例えば、続く文字列を想像:特定の2つの数字の次の6つの数字を取得

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

私は何を取得することである:それはあなたが欲しいものだ場合は、空間に\sを変更することができます

24824750 99659440 24234423 24743534 
+0

を次のようにあなたがやることな環境とは何ですか?ここでは2つのステップを使用する必要があります:スペースを削除し、一致を取得します。 –

+0

@WiktorStribiżewJavascript、ありがとうございました。 –

+0

偉大な、あなたはこれまで運があったか?あなたのコードを共有できますか? –

答えて

1
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,'')); 
} 

。それを行うには

0

別の方法(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);

0

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);

関連する問題