--- title: RegExp.prototype.source slug: Web/JavaScript/Reference/Global_Objects/RegExp/source tags: - JavaScript - Property - Prototype - Reference - RegExp - Regular Expressions translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/source --- <div>{{JSRef}}</div> <p><strong><code>source</code></strong> プロパティは、正規表現オブジェクトのソーステキストを含む {{jsxref("String")}} を返します。これには、両端の 2 つのスラッシュやフラグは含まれません。</p> <div>{{EmbedInteractiveExample("pages/js/regexp-prototype-source.html")}}</div> <div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> <div>{{js_property_attributes(0, 0, 1)}}</div> <h2 id="Examples" name="Examples">例</h2> <h3 id="Using_source" name="Using_source">source の使用</h3> <pre class="brush: js notranslate">var regex = /fooBar/ig; console.log(regex.source); // "fooBar", /.../ と "ig" は含まれません。 </pre> <h3 id="Empty_regular_expressions_and_escaping" name="Empty_regular_expressions_and_escaping">空の正規表現とエスケープ</h3> <p>ECMAScript 5 から、<code>source</code> プロパティは空の正規表現に対して空文字を返さなくなりました。代わりに、<code>(?:)</code> 文字列を返します。加えて、 ("\n" のような) 行区切りはエスケープされます。</p> <pre class="brush: js notranslate">new RegExp().source; // "(?:)" new RegExp('\n').source === '\n'; // ES5 以前で、true new RegExp('\n').source === '\\n'; // ES5 から、true </pre> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.source', 'RegExp.prototype.source')}}</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <div> <p>{{Compat("javascript.builtins.RegExp.source")}}</p> </div> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{jsxref("RegExp.prototype.flags")}}</li> </ul>