--- title: RegExp.prototype.exec() slug: Web/JavaScript/Reference/Global_Objects/RegExp/exec tags: - JavaScript - Method - Prototype - Reference - RegExp - Regular Expressions translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/exec --- <div>{{JSRef}}</div> <p><span class="seoSummary"><strong><code>exec()</code></strong> メソッドは、指定された文字列内で一致するものの検索を実行します。結果の配列、または {{jsxref("null")}} を返します。</span></p> <p>JavaScript の {{jsxref("RegExp")}} オブジェクトは、 {{jsxref("RegExp.global", "global")}} または {{jsxref("RegExp.sticky", "sticky")}} フラグが設定されている場合 (例えば <code>/foo/g</code> や <code>/foo/y</code>) は<strong>ステートフル</strong>になります。これは前回の一致位置を {{jsxref("RegExp.lastIndex", "lastIndex")}} に格納します。これを内部的に使用することで、 <code>exec()</code> はテキストの文字列内で (キャプチャグループのある) 複数の一致を反復処理することができます。これは単なる文字列の一致を取得する {{jsxref("String.prototype.match()")}} とは対照的です。</p> <p>より新しい関数として、 (キャプチャグループによる) 複数の部分の文字列の一致を単純化する {{jsxref("String.prototype.matchAll()")}} が提案されています。</p> <p>単に見つかったか見つからなかったかを <code>true</code> または <code>false</code> で知るために検索を実行するのであれば、 {{jsxref("RegExp.prototype.test()")}} メソッドまたは {{jsxref("String.prototype.search()")}} メソッドを使用してください。</p> <div>{{EmbedInteractiveExample("pages/js/regexp-prototype-exec.html")}}</div> <p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p> <h2 id="Syntax" name="Syntax">構文</h2> <pre class="syntaxbox notranslate"><var>regexObj</var>.exec(<var>str</var>)</pre> <h3 id="Parameters" name="Parameters">引数</h3> <dl> <dt><code><var>str</var></code></dt> <dd>正規表現に一致するかどうかの対象となる文字列。</dd> </dl> <h3 id="Return_value" name="Return_value">返値</h3> <p>一致に成功した場合、 <code>exec()</code> メソッドは配列を返し (追加のプロパティ <code>index</code> と <code>input</code> 付き、以下参照)、正規表現オブジェクトの {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティを更新します。返された配列は、一致したテキストを最初の項目として持ち、その後、一致したテキストの括弧によるキャプチャグループに対して 1 つずつの項目を持ちます。</p> <p>一致に失敗した場合は、 <code>exec()</code> メソッドは {{jsxref("null")}} を返し、 {{jsxref("RegExp.lastIndex", "lastIndex")}} を <code>0</code> に設定します。</p> <h2 id="Description" name="Description">解説</h2> <p>次の例を想定してください。</p> <pre class="brush: js notranslate">// "quick brown" の後に "jumps" が来るものを、その間の文字を無視して一致させます。 // "brown" と "jumps" を取得します。 // 大文字と小文字は区別しません。 let re = /quick\s(brown).+?(jumps)/ig; let result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog'); </pre> <p>このスクリプトの結果は以下の表の通りです。</p> <table class="fullwidth-table standard-table"> <thead> <tr> <th scope="row">オブジェクト</th> <th scope="col">プロパティ/添字</th> <th scope="col">説明</th> <th scope="col">例</th> </tr> </thead> <tbody> <tr> <th colspan="1" rowspan="4" scope="row" style="vertical-align: top;"><code>result</code></th> <td><code>[0]</code></td> <td>文字が一致した部分の文字列全体</td> <td><code>"Quick Brown Fox Jumps"</code></td> </tr> <tr> <td><code>[1], ...[<var>n</var>]</code></td> <td> <p>もしあれば、括弧に囲まれた部分文字列が一致したものです。</p> <p>括弧に囲まれた部分文字列の数に制限はありません。</p> </td> <td> <p><code>result[1] === "Brown"</code></p> <p><code>result[2] === "Jumps"</code></p> </td> </tr> <tr> <td><code>index</code></td> <td>0 から始める一致した文字列の位置。</td> <td><code>4</code></td> </tr> <tr> <td><code>input</code></td> <td>検索対象となった元の文字列。</td> <td><code>The Quick Brown Fox Jumps Over The Lazy Dog</code></td> </tr> <tr> <th colspan="1" rowspan="5" scope="row" style="vertical-align: top;"><code>re</code></th> <td><code>lastIndex</code></td> <td> <p>次回の検索を始める位置です。</p> </td> <td><code>g</code> がない場合は <code>0</code> のままです。</td> <td><code>25</code></td> </tr> <tr> <td><code>ignoreCase</code></td> <td>大文字小文字を区別しない、 <code>i</code> フラグが指定されているかどうか。</td> <td><code>true</code></td> </tr> <tr> <td><code>global</code></td> <td>グローバルマッチのための、 <code>g</code> フラグが指定されているかどうか。</td> <td><code>true</code></td> </tr> <tr> <td><code>multiline</code></td> <td>複数行に渡って文字列を検索する、 <code>m</code> フラグが指定されているかどうか。</td> <td><code>false</code></td> </tr> <tr> <td><code>source</code></td> <td>パターンの文字列。</td> <td><code>quick\s(brown).+?(jumps)</code></td> </tr> </tbody> </table> <h2 id="Examples" name="Examples">例</h2> <h3 id="Finding_successive_matches" name="Finding_successive_matches">成功する一致の検索</h3> <p>正規表現で "<code>g</code>" フラグを使用する場合、同じ文字列で成功する一致を見つけるために <code>exec()</code> メソッドを複数回使うことができます。その際、検索は正規表現オブジェクトの {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティで指定された位置の <code><var>str</var></code> の部分文字列から始まります ({{jsxref("RegExp.prototype.test()", "test()")}} も {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティの位置から始めます)。なお、別な文字列を検索する場合でも {{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティはリセットされず、既存の {{jsxref("RegExp.lastIndex", "lastIndex")}} から検索を始めます。</p> <p>例えば、次のスクリプトを考えてみてください。</p> <pre class="brush: js notranslate">let myRe = /ab*/g; let str = 'abbcdefabh'; let myArray; while ((myArray = myRe.exec(str)) !== null) { let msg = myArray[0] + ' を見つけました。'; msg += '次の検索は ' + myRe.lastIndex + ' からです。'; console.log(msg); } </pre> <p>このスクリプトは以下のテキストを表示します。</p> <pre class="notranslate">abb を見つけました。次の検索は 3 からです。 ab を見つけました。次の検索は 9 からです。 </pre> <div class="warning"> <p><strong>警告:</strong> <strong>正規表現リテラル (または {{jsxref("RegExp")}} コンストラクター) を <code>while</code> の条件の中に配置しないでください。</strong></p> <p><strong>{{jsxref("RegExp.lastIndex", "lastIndex")}} プロパティが繰り返し毎にリセットされるので、無限ループになります。</strong></p> <p><strong>また、グローバルフラグ ("<code>g</code>") が設定されていることを確認してください。これも無限ループを引き起こします。</strong></p> </div> <h3 id="Using_exec_with_RegExp_literals" name="Using_exec_with_RegExp_literals"><strong>RegExp リテラルでの exec() の使用</strong></h3> <p><strong>{{jsxref("RegExp")}} オブジェクトを作成せずに <code>exec()</code> を使用することもできます。</strong></p> <pre class="brush: js notranslate"><strong>let matches = /(hello \S+)/.exec('This is a hello world!'); console.log(matches[1]); </strong></pre> <p><strong>これで 'hello world!' を含んだメッセージをログ出力します。</strong></p> <h2 id="Specifications" name="Specifications"><strong>仕様書</strong></h2> <table class="standard-table"> <thead> <tr> <th scope="col"><strong>仕様書</strong></th> </tr> </thead> <tbody> <tr> <td><strong>{{SpecName('ESDraft', '#sec-regexp.prototype.exec', 'RegExp.exec')}}</strong></td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility"><strong>ブラウザーの互換性</strong></h2> <div> <div class="hidden"><strong>このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</strong></div> <p><strong>{{Compat("javascript.builtins.RegExp.exec")}}</strong></p> </div> <h2 id="See_also" name="See_also"><strong>関連情報</strong></h2> <ul> <li><strong><a href="/ja/docs/Web/JavaScript/Guide">JavaScript ガイド</a> の<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現</a>の章</strong></li> <li><strong>{{jsxref("RegExp")}}</strong></li> </ul>