JavaScript文字列match()
例
文字列を使用した「ain」の検索:
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
正規表現を使用した「ain」の検索:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
「ain」の世界的な調査:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
グローバルで大文字と小文字を区別しない検索:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
定義と使用法
このmatch()
メソッドは、文字列を正規表現と照合します**
このmatch()
メソッドは、一致する配列を返します。
一致するものが見つからない場合、メソッドmatch()
はnullを返します。
構文
string.match(match)
パラメーター
Parameter | Description |
match | Required. The search value. A regular expression (or a string that will be converted to a regular expression). |
戻り値
タイプ | 説明 |
配列 または null | 一致を含む配列。null 一致するものが見つからない場合。 |
文字列match()と文字列search()の違い
このmatch()
メソッドは、一致の配列を返します。
このsearch()
メソッドは、最初の一致の位置を返します。
正規表現の検索方法
JavaScriptでは、正規表現のテキスト検索をさまざまな方法で実行できます。
正規表現としてのパターンの場合、これらは最も一般的な方法です。
例 | 説明 |
---|---|
text.match(パターン) | Stringメソッドmatch() |
text.search(パターン) | 文字列メソッドsearch() |
パターン.exec(テキスト) | RexExpメソッドexec() |
パターン.test(テキスト) | RegExpメソッドtest() |
ブラウザのサポート
match()
ECMAScript1(ES1)機能です。
ES1(JavaScript 1997)は、すべてのブラウザーで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |