SORU
9 Mayıs 2009, CUMARTESİ


JavaScript düzenli ifadeler ve alt maçlar

Neden Javascript alt maçlar g değiştirici etkin olduğunda çalışmamaya mı?

var text = 'test test test test';

var result = text.match(/t(e)(s)t/);
// Result: ["test", "e", "s"]

Yukarıda işler iyi, result[1] "e" result[2] "s".

var result = text.match(/t(e)(s)t/g);
// Result: ["test", "test", "test", "test"]

Yukarıdaki yakalayan benim grupları yok sayıyor. Aşağıdaki tek geçerli Çözüm mü?

var result = text.match(/test/g);
for (var i in result) {
    console.log(result[i].match(/t(e)(s)t/));
}
/* Result:
["test", "e", "s"]
["test", "e", "s"]
["test", "e", "s"]
["test", "e", "s"]
*/

CEVAP
9 Mayıs 2009, CUMARTESİ


Kullanarak String'match() fonksiyon s öğrendiğin gibi küresel değiştirici etkin olduğunda çekilen grupları geri dönüş olmaz.

Bu durumda, RegExp bir nesne kullanın ve exec() fonksiyonunu çağırmak isterim. String'match() RegExphemen hemen aynı'exec() s...durumlar dışında bu işlevi. Küresel değiştirici RegExp iken, match()normal işlevi çekilen grupları geri ödemiyor, ayarlanır'exec() fonksiyon s olacak. (here, diğer yerler arasında. dikkat)

Hatırlanması gereken başka bir yakalamak exec() bu durumda null döner dizi—dolana kadar maçlar dönen tutar, büyük maçlar dönüş olmamasıdır.

Bu yüzden, örneğin, şöyle bir şey yapabilirsin:

var pattern = /t(e)(s)t/g;  // Alternatively, "new RegExp('t(e)(s)t', 'g');"
var match;    

while (match = pattern.exec(text)) {
    // Do something with the match (["test", "e", "s"]) here...
}

Unutulmaması gereken bir diğer şey RegExp.prototype.exec() RegExp.prototype.test() sağlanan dize düzenli ifade yürütme ve ilk sonuç bu. Her ardışık arama sonuç kümesi dizedeki geçerli dayalı RegExp.prototype.lastIndex güncelleme adım olacaktır.

İşte size bir örnek: // 4 örnek ve desen maç var unutmayın. lastındex 0'da başlar

pattern.test(text); // pattern.lastIndex = 4
pattern.test(text); // pattern.lastIndex = 9
pattern.exec(text); // pattern.lastIndex = 14
pattern.exec(text); // pattern.lastIndex = 19

// if we were to call pattern.exec(text) again it would return null and reset the pattern.lastIndex to 0
while (var match = pattern.exec(text)) {
    // never gets run because we already traversed the string
    console.log(match);
}

pattern.test(text); // pattern.lastIndex = 4
pattern.test(text); // pattern.lastIndex = 9

// however we can reset the lastIndex and it will give us the ability to traverse the string from the start again or any specific position in the string
pattern.lastIndex = 0;

while (var match = pattern.exec(text)) {
    // outputs all matches
    console.log(match);
}

34* *RegExp nesneleri nasıl kullanılacağı hakkında bilgi (özellikle, burada the exec() function belgeleri) bulabilirsiniz.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • hoorahjencar

    hoorahjencar

    6 HAZİRAN 2007
  • Malwarebytes

    Malwarebytes

    22 Temmuz 2007
  • Microsoft Research

    Microsoft Re

    24 EKİM 2008