JavaScript တွင် Regular Expression Variable
Regular expression များကို variable များထဲသို့ထုတ်၍သိမ်းဆည်းနိုင်ပါသည်။ ဥပမာတစ်ခုကြည့်ရအောင်။ ကျွန်ုပ်တို့တွင် အောက်ပါ string တစ်ခုရှိသည်ဆိုပါစို့။
let str = 'aaa bbb ccc';
အောက်ပါ regular expression ပါဝင်သော code တစ်ခုကျွန်ုပ်တို့တွင်ရှိသည်ဆိုပါစို့။
let res = str.replace(/\w+/, '!');
ထို regular expression ကို variable တစ်ခုထဲသို့ထုတ်၍ထားကြည့်ပါမည်။
let reg = /\w+/;
let res = str.replace(reg, '!');
အောက်ပါ code တွင် regular expression ကို variable တစ်ခုထဲသို့ထုတ်၍ထားပါ။
let str = 'a aa aaa aaaa aaaa';
let res = str.replace(/a+/, '!');