matchAll Method

matchAll method သည် regular expression နှင့် ကိုက်ညီသော အရာအားလုံးကို iterable object အဖြစ် ပြန်ပေးပြီး ၎င်း၏ element တစ်ခုစီသည် တွေ့ရှိခဲ့သည့် အရာ နှင့် ၎င်း၏ capturing groups များပါဝင်သော array ကို ပါဝင်ပါသည်။ Method ကို g modifier ဖြင့်သာ အသုံးပြုနိုင်ပါသည်။ ကိုက်ညီမှုမရှိပါက null ပြန်ပေးမည်။

Syntax

javascriptstring.matchAll(regular expression);

ဥပမာ

ကိုက်ညီမှုအားလုံးကို ရယူပြီး loop ဖြင့် ထပ်ကျော့ကြည့်ကြပါစို့:

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

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

Code ကို run လိုက်သော အခါ ရလဒ်:

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

ဥပမာ

Iterable object ကို ပုံမှန် array အဖြစ်သို့ ပြောင်းလဲကြည့်ကြပါစို့:

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

console.log(res);

Code ကို run လိုက်သော အခါ ရလဒ်:

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

ဆက်လက်လေ့လာရန်

  • test method,
    သည် string ကို စစ်ဆေးပေးသည်
  • match method,
    သည် string ထဲတွင် ကိုက်ညီမှုကို ရှာဖွေပေးသည်
  • exec method,
    သည် အစဉ်လိုက် ရှာဖွေမှု ပြုလုပ်ပေးသည်
  • replace method,
    သည် ရှာဖွေခြင်း နှင့် အစားထိုးခြင်းကို ပြုလုပ်ပေးသည်
  • search method,
    သည် ရှာဖွေမှုကို ပြုလုပ်ပေးသည်
  • split method,
    သည် string ကို ပိုင်းခြားပေးသည်