JavaScript ရှိ Regular Expressions တွင် lastIndex ဂုဏ်သတ္တိ
Regular Expression တစ်ခုတွင်
lastIndex ဟုခေါ်သော ဂုဏ်သတ္တိတစ်ခုရှိသည်။
ထိုဂုဏ်သတ္တိသည် exec method နောက်တစ်ကြိမ်
အခေါ်ခံရသောအခါ ရှာဖွေရာတွင် စတင်မည့်နေရာအညွှန်းကို
သိမ်းဆည်းထားသည်။
ဆိုလိုသည်မှာ method ကို အသစ်ခေါ်ယူတိုင်းတွင်
ဤဂုဏ်သတ္တိ၏တန်ဖိုး ပြောင်းလဲသွားမည်ဖြစ်သည်။
ဥပမာတစ်ခုဖြင့် ကြည့်ရှုကြပါစို့။
let str = '12 34 56';
let reg = /\d+/g;
console.log(reg.lastIndex); // ကနဦးတန်ဖိုး 0
let res;
while (res = reg.exec(str)) {
console.log(res); // [12], [34], [56]
console.log(reg.lastIndex); // 2, 5, 8
}
lastIndex ၏ အားသာချက်မှာ
၎င်းကို ဖတ်ရှုရုံသာမက
ပြောင်းလဲသတ်မှတ်နိုင်ပြီး၊ သတ်မှတ်ထားသော
နေရာမှ စတင်၍ ရှာဖွေနိုင်ခြင်းပင် ဖြစ်သည်။
အောက်ပါဥပမာကို ကြည့်ပါ။
let str = '12 34 56';
let reg = /\d+/g;
reg.lastIndex = 2;
let res = reg.exec(str)
console.log(res); // [34]
အောက်ပါ string ကို ပေးထားသည်။
let str = '12:37 15:48 17:59';
ပဉ္စမမြောက် စာလုံးမှစ၍ ထို string အတွင်းရှိ အချိန်ပြသော substring အားလုံးကို ရှာဖွေပါ။