JScript 10.0
match メソッド
この記事は翻訳者によって翻訳されたものです。 このページおよび元の英語コンテンツを同時に表示させるには、[ライトウェイト] に切り替えます。
正規表現パターンを使って文字列に対して検索を実行し、検索結果を配列に格納して返します。
function match(rgExp : RegExp) : Array
引数
解説
パターンに一致する文字列が見つからなかった場合、match メソッドは null を返します。 一致する文字列が見つかった場合、match は配列を返し、さらにグローバルな RegExp オブジェクトのプロパティが検索結果を反映して更新されます。
match メソッドが返す配列には、input、index、および lastIndex. の 3 つのプロパティがあります。 input プロパティには、検索された文字列全体が格納されます。 index プロパティには、検索された文字列内の一致した部分文字列の位置が格納されます。 lastIndex プロパティには、最後に一致したサブストリングの最後の文字の直後の位置が格納されます。
グローバル フラグ (g) が設定されていない場合、配列の要素 0 には一致した文字列全体が、要素 1 から n には、一致した文字列の中に副次的に含まれている内容が格納されます。 この処理は、グローバル フラグが設定されていない場合の exec メソッド メソッドの処理と同じです。 グローバル フラグが設定されている場合、要素 0 から n には一致したすべての文字列がそれぞれ格納されます。
使用例
グローバル フラグ (g) が設定されていない場合の match メソッドの使用例を次に示します。
JScript
var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!"; // Create a regular expression to search for an e-mail address. // The global flag is not included. // (More sophisticated RegExp patterns are available for // matching an e-mail address.) var re = /(\w+)@(\w+)\.(\w+)/; var result = src.match(re); // Because the global flag is not included, the entire match is // in array element 0, and the submatches are in elements 1 through n. print(result[0]); for (var index = 1; index < result.length; index++) { print("submatch " + index + ": " + result[index]); } // Output: // george@contoso.com // submatch 1: george // submatch 2: contoso // submatch 3: com
グローバル フラグ (g) を設定した場合の match メソッドの使用例を次に示します。
JScript
var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!"; // Create a regular expression to search for an e-mail address. // The global flag is included. var re = /(\w+)@(\w+)\.(\w+)/g; var result = src.match(re); // Because the global flag is included, the matches are in // array elements 0 through n. for (var index = 0; index < result.length; index++) { print(result[index]); } // Output: // george@contoso.com // someone@example.com
match メソッドの文字列リテラルの使用例を次に示します。
JScript
var re = /th/i; var result = "through the pages of this book".match(re);
必要条件
参照