「JavaScript」testメソッド正規表現で文字OR指定を行うサンプル

書式
変数名 = /[文字1文字2]/;
文字1または文字2を指定しています。
正規表現のパターンで文字列を検索します。
一致した場合はtrueを返し、一致しなかった場合falseを返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const rr = /[ST]/;
console.log(rr.test("STS"));
console.log(rr.test("SUU"));
console.log(rr.test("KKK"));
const rr = /[ST]/; console.log(rr.test("STS")); console.log(rr.test("SUU")); console.log(rr.test("KKK"));
const rr = /[ST]/;

console.log(rr.test("STS"));
console.log(rr.test("SUU"));
console.log(rr.test("KKK"));

結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
true
true
false
true true false
true
true
false

 

JavaScript

Posted by arkgame