JavaScript exec()メソッド
JavaScript RegExp オブジェクト
定義と使用方法
exec() メソッドは、文字列内の正規表現のマッチを検索するために使用されます。
文字列にマッチする値があればそのマッチ値を返し、なければ null を返します。
構文
RegExpObject.exec(string)
| 引数 | 説明 |
|---|---|
| string | Required. The string to be searched |
ブラウザサポート
![]()
![]()
![]()
![]()
![]()
すべての主要ブラウザが exec() メソッドをサポートしています。
例
例
文字列内で "Hello" と "サンプル" をグローバル検索します:
var str="Hello world!";
//"Hello" を検索
var patt=/Hello/g;
var result=patt.exec(str);
document.write("戻り値:" + result);
//"サンプル" を検索
patt=/サンプル/g;
result=patt.exec(str);
document.write("<br>戻り値:" + result);
上記の例の出力結果:
Returned value: Hello
Returned value: null
Returned value: null
試してみる »
JavaScript RegExp オブジェクト その他の拡張