JavaScript တွင် Regular Expression Variable

Regular expression များကို variable များထဲသို့ထုတ်၍သိမ်းဆည်းနိုင်ပါသည်။ ဥပမာတစ်ခုကြည့်ရအောင်။ ကျွန်ုပ်တို့တွင် အောက်ပါ string တစ်ခုရှိသည်ဆိုပါစို့။

javascriptlet str = 'aaa bbb ccc';

အောက်ပါ regular expression ပါဝင်သော code တစ်ခုကျွန်ုပ်တို့တွင်ရှိသည်ဆိုပါစို့။

javascriptlet res = str.replace(/\w+/, '!');

ထို regular expression ကို variable တစ်ခုထဲသို့ထုတ်၍ထားကြည့်ပါမည်။

javascriptlet reg = /\w+/;
let res = str.replace(reg, '!');
Задание 1

အောက်ပါ code တွင် regular expression ကို variable တစ်ခုထဲသို့ထုတ်၍ထားပါ။

javascriptlet str = 'a aa aaa aaaa aaaa';
let res = str.replace(/a+/, '!');