W3cubDocs

/JavaScript

regExp.source

The source property returns a String containing the source text of the regexp object, and it doesn't contain the two forward slashes on both sides and any flags.

Property attributes of RegExp.prototype.source
Writable no
Enumerable no
Configurable yes

Examples

Using source

var regex = /fooBar/ig;

console.log(regex.source); // "fooBar", doesn't contain /.../ and "ig".

Empty regular expressions and escaping

Starting with ECMAScript 5, the source property no longer returns an empty string for empty regular expressions. Instead, the string (?:) is returned. In addition, line terminators (such as "\n") are escaped now.

new RegExp().source; // "(?:)"

new RegExp('\n').source === '\n';  // true, prior to ES5
new RegExp('\n').source === '\\n'; // true, starting with ES5

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
source 1 12 1 4 5 1
"(?:)" for empty regexps 6 12 38 No 15 5
Escaping 73 12 38 10 60 6
Prototype accessor property (ES2015) 48 12 41 4 35 1.3
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
source 1 18 4 10.1 1 1.0
"(?:)" for empty regexps ≤37 18 38 14 4.2 1.0
Escaping 73 73 38 52 6 No
Prototype accessor property (ES2015) 48 48 41 35 1 5.0
Server
Node.js
source 0.1.100
"(?:)" for empty regexps 0.8.0
Escaping 12.0.0
Prototype accessor property (ES2015) 6.0.0

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source