matchAll Metodu

matchAll metodu, düzenli ifadeyle tüm eşleşmeleri, her bir elemanı bulunan eşleşmeyi ve gruplarını içeren yinelenebilir bir nesne olarak döndürür. Metot sadece g değiştiricisiyle kullanılabilir. Eşleşme yoksa null döndürür.

Sözdizimi

javascriptstring.matchAll(düzenli ifade);

Örnek

Tüm eşleşmeleri alalım ve bir döngüyle üzerlerinden geçelim:

javascriptlet str = '12 34 56';
let matches = str.matchAll(/(\d)(\d)/g);

for (let match of matches) {
  console.log(match);
}

Kodun çalıştırılmasının sonucu:

javascript[12, 1, 2]
[34, 3, 4]
[56, 5, 6] 

Örnek

Yinelenebilir nesneyi normal bir diziye dönüştürelim:

javascriptlet str = '12 34 56';
let matches = str.matchAll(/(\d)(\d)/g);
let res = Array.from(matches);

console.log(res);

Kodun çalıştırılmasının sonucu:

javascript[
  [12, 1, 2],
  [34, 3, 4],
  [56, 5, 6]
]

Ayrıca Bakınız

  • metot test,
    bir stringi doğrular
  • metot match,
    bir string içinde eşleşmeleri arar
  • metot exec,
    sıralı arama yapar
  • metot replace,
    arama ve değiştirme yapar
  • metot search,
    arama yapar
  • metot split,
    bir stringi böler