testメソッド
変数1 = /正規表現のパターン/; 変数 = 変数1.test(文字列); |
- 正規表現のパターンで文字列を検索します。
- 一致した場合はtrueを返し、一致しなかった場合falseを返します。
- RegExpオブジェクトのメソッドです。
特定のパターンが存在するか確認するだけであればmatch/execメソッドではなくsearch/testメソッドを使用します。testメソッドは戻り値が真偽値なので実行速度が早くなります。
文字列を指定
<script>
const a = /BC/;
console.log(a.test("ABC")); //true
console.log(a.test("ACB")); //false
</script>
2行目は、文字列「BC」を指定しています。
任意の1文字(.)
const a = /.BC/;
console.log(a.test("ABC")); //true
console.log(a.test("AABC")); //true
console.log(a.test("ACB")); //false
ピリオド(.)は、任意の1文字を表します。
任意の文字が0回以上(.*)
const a = /A.*D/;
console.log(a.test("ABCD")); //true
console.log(a.test("ABD")); //true
console.log(a.test("AD")); //true
ドット(.)は任意の文字でアスタリスク(*)は直前の文字が0回以上の繰り返しを表します。
任意の文字が1回以上(.+)
const a = /A.+D/;
console.log(a.test("ABCD")); //true
console.log(a.test("ABD")); //true
console.log(a.test("AD")); //false
ドット(.)は任意の文字でプラス(+)は直前の文字が1回以上の繰り返しを表します。
orを指定([])
const a = /[AB]/;
console.log(a.test("ABC"));//true
console.log(a.test("ZZB"));//true
console.log(a.test("ZZZ"));//false
「A」または「B」を指定しています。
否定(^)
const a = /[^A]/;
console.log(a.test("ZZZ"));//true
console.log(a.test("ZAA"));//true
console.log(a.test("AAA"));//false
「A」以外の文字を指定しています。
先頭の文字列で検索する正規表現と似ていますが、こちらは角かっこ([])があります。
文字の範囲(-)
const a = /[A-C]/;
console.log(a.test("ABC"));//true
console.log(a.test("ZZC"));//true
console.log(a.test("ZZZ"));//false
「A」または「B」または「C」の文字を指定しています。
先頭の文字列(^)
const a = /^AB/;
console.log(a.test("ABC"));//true
console.log(a.test("ACB"));//false
先頭が「AB」の文字を指定しています。
文字を否定で検索する正規表現と似ていますが、こちらは角かっこ([])がありません。
末尾の文字列($)
const a = /BC$/;
console.log(a.test("ABC"));//true
console.log(a.test("ACB"));//false
末尾が「BC」の文字を指定しています。
連続する文字{数値}
const a = /c{2}/;
console.log(a.test("acccb"));//true
console.log(a.test("accb"));//true
console.log(a.test("acbc"));//false
「c」が2回連続する文字を指定しています。
1桁以上{1,}
const ptr = /[0-9]{1,}-/;
console.log(ptr.test("1-"));// true
console.log(ptr.test("123-")); // true
console.log(ptr.test("ab1-")); // true
console.log(ptr.test("a1b-")); // false
数値1桁以上とハイフン(-)を指定しています。
数値(0-9)の5桁+先頭と末尾も(0-9)
const a = /^[0-9]{5}$/;
console.log(a.test("12345")); //true
console.log(a.test("123")); //false 桁数不足
console.log(a.test("a2345")); //false 文字あり
数値(0-9)と英字(a-f)の5桁+先頭と末尾も(0-9 or a-f)
const a = /^[0-9a-f]{5}$/;
console.log(a.test("1a23b"));//true
console.log(a.test("a1bc"));//false 桁数不足
console.log(a.test("12345"));//true 数値のみ
console.log(a.test("abcde"));//true 英字のみ
郵便番号(数値3桁とハイフンと数値4桁)+先頭と末尾も(0-9)
const a = /^\d{3}-\d{4}$/;
console.log(a.test("123-4567"));//true
console.log(a.test("1234567"));//false
console.log(a.test("1234-567"));//false
2行目の\dは、数値を意味します。[0-9]と同じです。
ドット(.)を文字として指定(エスケープ)
const ptr = /a\./;
console.log(ptr.test("a."));// true
console.log(ptr.test("aa")); // False
2行目は、ドット(.)を文字として扱います。「a.」を指定しています。
エスケープとしてバックスラッシュ(ユニコードのU+005C)を前につけます。環境によっては円マークで表示されます。
ドット(.)にエスケープをつけない場合は任意の一文字という意味になります。
以下はMDNのtestメソッドのリンクです。
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
正規表現の構文とオプション
正規表現の構文として以下2つがあります。
変数 = /正規表現のパターン/オプション |
変数 = new RegExp('正規表現のパターン','オプション') |
上記のサンプルコードは全て正規表現リテラルを使用しています。
1つめは、正規表現リテラルです。
2つめは、RegExpオブジェクトのコンストラクタです。
主なオプション
オプション | 説明 |
---|---|
g | マッチしたものをすべて返す(Global test.) |
i | 大文字と小文字の区別をしない(Case-insensitive test.) |
m | 複数行をマッチする |
正規表現のパターン
関連の記事
JavaScript replace 文字列を置換する(正規表現)
JavaScript search 正規表現でインデックス値を返す