--- 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 ---
source プロパティは、正規表現オブジェクトのソーステキストを含む {{jsxref("String")}} を返します。これには、両端の 2 つのスラッシュやフラグは含まれません。
var regex = /fooBar/ig; console.log(regex.source); // "fooBar", /.../ と "ig" は含まれません。
ECMAScript 5 から、source プロパティは空の正規表現に対して空文字を返さなくなりました。代わりに、(?:) 文字列を返します。加えて、 ("\n" のような) 行区切りはエスケープされます。
new RegExp().source; // "(?:)"
new RegExp('\n').source === '\n'; // ES5 以前で、true
new RegExp('\n').source === '\\n'; // ES5 から、true
| 仕様書 |
|---|
| {{SpecName('ESDraft', '#sec-get-regexp.prototype.source', 'RegExp.prototype.source')}} |
{{Compat("javascript.builtins.RegExp.source")}}