--- title: String.prototype.match() slug: Web/JavaScript/Reference/Global_Objects/String/match tags: - JavaScript - Method - Prototype - Reference - Regular Expressions - String - メソッド - 正規表現 translation_of: Web/JavaScript/Reference/Global_Objects/String/match ---
{{JSRef}}

match() メソッドは、正規表現に対する文字列のマッチングの結果を受け取ります。

{{EmbedInteractiveExample("pages/js/string-match.html", "shorter")}}

構文

str.match(regexp)

引数

regexp
正規表現オブジェクトです。
regexpRegExp ではないオブジェクトであった場合、 {{jsxref("RegExp")}} への暗黙的な変換が new RegExp(regexp) を使用して行われます。
一切引数を与えずに match() メソッドを使った場合、空の文字列 1 つを持つ {{jsxref("Array")}}、 [""] が得られます。

返値

グローバル (g) フラグの有無によって内容が変わる {{jsxref("Array")}} を返します。一致するものが見つからなかった場合は {{jsxref("null")}} を返します。

追加のプロパティ

上記の説明にある通り、結果は追加のプロパティを含むことがあります。

groups
名前付きキャプチャグループのオブジェクトで、キーは名前、値はキャプチャグループ、または名前付きキャプチャグループが定義されていない場合は {{jsxref("undefined")}} です。詳細はグループと範囲を見てください。
index
結果が見つかった検索のインデックスです。
input
検索された文字列のコピーです。

解説

正規表現が g フラグを含んでいない場合、 str.match() は {{jsxref("RegExp.prototype.exec()", "RegExp.exec()")}} と同じ結果を返します。

その他のメソッド

match() の使用

以下の例において、 match() は 'Chapter' とそれに続く 1 桁以上の数字、それに続く 0 回以上の小数点と数字を見つけるために使われています。

正規表現が i フラグを含んでいるので、大文字と小文字の違いは無視されます。

let str = 'For more information, see Chapter 3.4.5.1';
let re = /see (chapter \d+(\.\d)*)/i;
let 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' is the whole match.
// 'Chapter 3.4.5.1' was captured by '(chapter \d+(\.\d)*)'.
// '.1' was the last value captured by '(\.\d)'.
// The 'index' property (22) is the zero-based index of the whole match.
// The 'input' property is the original string that was parsed.

match() での g と i フラグの使用

以下の例は、 g と i フラグを match() で使用した実例です。 A から E までと、 a から e までのすべての文字が返され、それぞれが配列の個々の要素に入ります。

let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let regexp = /[A-E]/gi;
let matches_array = str.match(regexp);

console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

メモ: {{jsxref("String.prototype.matchAll()")}} とフラグを用いた高度な検索も参照してください。

名前付きキャプチャグループの使用

名前付きキャプチャグループに対応しているブラウザーでは、次のコードは "fox" または "cat" を "animal" という名前のグループに入れます。

let paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';

let capturingRegex = /(?<animal>fox|cat) jumps over/;
found = paragraph.match(capturingRegex);
console.log(found.groups); // {animal: "fox"}

引数なしの match() の使用

let str = "Nothing will come of nothing.";

str.match();   // returns [""]

RegExp ではないオブジェクトを引数にする

引数 regexp が文字列または数値である場合、暗黙に new RegExp(regexp) を使用して {{jsxref("RegExp")}} に変換されます。

正の符号がついた正の数であった場合、 RegExp() は正の符号を無視します。

let 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" is a string. returns ["number"]
str1.match(NaN);        // the type of NaN is the number. returns ["NaN"]
str1.match(Infinity);   // the type of Infinity is the number. returns ["Infinity"]
str1.match(+Infinity);  // returns ["Infinity"]
str1.match(-Infinity);  // returns ["-Infinity"]
str2.match(65);         // returns ["65"]
str2.match(+65);        // A number with a positive sign. returns ["65"]
str3.match(null);       // returns ["null"]

仕様書

仕様書
{{SpecName('ESDraft', '#sec-string.prototype.match', 'String.prototype.match')}}

ブラウザーの互換性

match() の基本対応

{{Compat("javascript.builtins.String.match")}}

名前付きキャプチャグループの対応

{{Compat("javascript.builtins.RegExp.named_capture_groups")}}

Firefox 特有のメモ

関連情報