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
string.matchAll(düzenli ifade);
Örnek
Tüm eşleşmeleri alalım ve bir döngüyle üzerlerinden geçelim:
let 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:
[12, 1, 2]
[34, 3, 4]
[56, 5, 6]
Örnek
Yinelenebilir nesneyi normal bir diziye dönüştürelim:
let 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:
[
[12, 1, 2],
[34, 3, 4],
[56, 5, 6]
]