JavaScript RegExp စမ်းသပ်မှု()
ဥပမာ
စာလုံး "e" အတွက် စာကြောင်းကို ရှာပါ-
let text = "The best things in life are free";
let pattern = /e/;
let result = pattern.test(text);
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
test() method သည် string တစ်ခုရှိ ကိုက်ညီမှုအတွက် စမ်းသပ်သည်။
ကိုက်ညီမှုတစ်ခုတွေ့ပါက အမှန်ပြန်ဖြစ်မည်၊ မဟုတ်ပါက false ပြန်ပေးသည်။
Browser ပံ့ပိုးမှု
test()
ECMAScript1 (ES1) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES1 (JavaScript 1997) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝ ပံ့ပိုးထားသည်။
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
အထားအသို
RegExpObject.test(string)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
string | Required. The string to be searched |
တန်ဖိုးကို ပြန်ပေးသည်။
Type | Description |
---|---|
Boolean | Returns true if it finds a match, otherwise false |
နောက်ထပ် ဥပမာများ
ဥပမာ
ကမ္ဘာလုံးဆိုင်ရာရှာဖွေမှုကိုပြုလုပ်ပြီး string တစ်ခုတွင် "Hello" နှင့် "W3Schools" အတွက် စမ်းသပ်ပါ။
// The string:
let text = "Hello world!";
// Look for "Hello"
let pattern1 = /Hello/g;
let result1 = patt.test(text);
// Look for "W3Schools"
let pattern2 = /W3Schools/g;
let result2 = pattern2.test(text);
ပုံမှန်ဖော်ပြမှုရှာဖွေရေးနည်းလမ်းများ
JavaScript တွင်၊ ပုံမှန် expression text search ကို မတူညီသောနည်းလမ်းများဖြင့် လုပ်ဆောင်နိုင်သည်။
ပုံမှန်အသုံးအနှုန်းအဖြစ် ပုံစံ တစ်ခု ဖြင့်၊ ဤအရာများသည် အသုံးအများဆုံးနည်းလမ်းများဖြစ်သည်-
ဥပမာ | ဖော်ပြချက် |
---|---|
text.match( ပုံစံ ) | String method သည် match() |
text.search( ပုံစံ ) | စာတန်းနည်းလမ်းရှာဖွေမှု() |
ပုံစံ .exec(စာသား) | RexExp နည်းလမ်း exec() |
ပုံစံ .test(စာသား) | RegExp နည်းလမ်းစမ်းသပ်မှု() |