String.prototype.match()
match()
メソッドは、正規表現に対する文字列の照合結果を受け取ります。
試してみましょう
構文
match(regexp)
引数
返値
解説
正規表現に g
フラグが付いていない場合、 str.match()
は RegExp.exec()
と同じ結果を返します。
その他のメソッド
- ある文字列が正規表現
RegExp
に一致するかどうかを知る必要がある場合は、RegExp.test()
を使用してください。 - 一番最初に一致したものだけが欲しい場合、代わりに
RegExp.exec()
を使ったほうが良いかもしれません。 - キャプチャグループを取得する場合でグローバルフラグが設定されていた場合は、
RegExp.exec()
を使用してください。
例
match() の使用
以下の例において、 match()
は 'Chapter
' とそれに続く 1 桁以上の数字、それに続く 0 回以上の小数点と数字を見つけるために使われています。
正規表現が i
フラグを含んでいるので、大文字と小文字の違いは無視されます。
const str = 'For more information, see Chapter 3.4.5.1';
const re = /see (chapter \d+(\.\d)*)/i;
const found = str.match(re);
console.log(found);
// logs [ 'see Chapter 3.4.5.1',
// 'Chapter 3.4.5.1',
// '.1',
// index: 22,
// input: 'For more information, see Chapter 3.4.5.1' ]
// 'see Chapter 3.4.5.1' は一致するもの全体です。
// 'Chapter 3.4.5.1' は '(chapter \d+(\.\d)*)' でキャプチャされたものです。
// '.1' は '(\.\d)' でキャプチャされた最後の値です。
// 'index' プロパティ (22) はゼロから始まる一致した位置です。
// 'input' プロパティは解釈された元の文字列です。
match() での g と i フラグの使用
以下の例は、 g と i フラグを match()
で使用した実例です。 A
から E
までと、 a
から e
までのすべての文字が返され、それぞれが配列の個々の要素に入ります。
const str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const regexp = /[A-E]/gi;
const matches_array = str.match(regexp);
console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
メモ: String.prototype.matchAll()
とフラグを用いた高度な検索も参照してください。
名前付きキャプチャグループの使用
名前付きキャプチャグループに対応しているブラウザーでは、次のコードは "fox
" または "cat
" を "animal
" という名前のグループに入れます。
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const capturingRegex = /(?<animal>fox|cat) jumps over/;
const found = paragraph.match(capturingRegex);
console.log(found.groups); // {animal: "fox"}
引数なしの match() の使用
const str = "Nothing will come of nothing.";
str.match(); // returns [""]
RegExp ではないオブジェクトを引数にする
引数 regexp
が文字列または数値である場合、暗黙に new RegExp(regexp)
を使用して RegExp
に変換されます。
正の符号がついた正の数であった場合、 RegExp()
は正の符号を無視します。
const str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
str2 = "My grandfather is 65 years old and My grandmother is 63 years old.",
str3 = "The contract was declared null and void.";
str1.match("number"); // "number" は文字列です。 ["number"] を返します。
str1.match(NaN); // NaN の型は数値です。 ["NaN"] を返します。
str1.match(Infinity); // Infinity の方は数値です。 ["Infinity"] を返します。
str1.match(+Infinity); // ["Infinity"] を返します
str1.match(-Infinity); // ["-Infinity"] を返します
str2.match(65); // ["65"] を返します
str2.match(+65); // 正の符号が付いた数値です。 ["65"] を返します
str3.match(null); // ["null"] を返します。
仕様書
Specification |
---|
ECMAScript Language Specification # sec-string.prototype.match |
ブラウザーの互換性
BCD tables only load in the browser