RegExp.prototype.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
Specification |
---|
ECMAScript Language Specification # sec-get-regexp.prototype.source |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
source | ChromeFull support1 | EdgeFull support12 | FirefoxFull support1 | Internet ExplorerFull support4 | OperaFull support5 | SafariFull support1 | WebView AndroidFull support1 | Chrome AndroidFull support18 | Firefox for AndroidFull support4 | Opera AndroidFull support10.1 | Safari on iOSFull support1 | Samsung InternetFull support1.0 | DenoFull support1.0 | Node.jsFull support0.10.0 |
"(?:)" for empty regexps | ChromeFull support6 | EdgeFull support12 | FirefoxFull support38 | Internet ExplorerNo supportNo | OperaFull support15 | SafariFull support5 | WebView AndroidFull support37 | Chrome AndroidFull support18 | Firefox for AndroidFull support38 | Opera AndroidFull support14 | Safari on iOSFull support4.2 | Samsung InternetFull support1.0 | DenoFull support1.0 | Node.jsFull support0.10.0 |
Escaping | ChromeFull support73 | EdgeFull support12 | FirefoxFull support38 | Internet ExplorerFull support10 | OperaFull support60 | SafariFull support6 | WebView AndroidFull support73 | Chrome AndroidFull support73 | Firefox for AndroidFull support38 | Opera AndroidFull support52 | Safari on iOSFull support6 | Samsung InternetFull support11.0 | DenoFull support1.0 | Node.jsFull support12.0.0 |
Prototype accessor property (ES2015) | ChromeFull support48 | EdgeFull support12 | FirefoxFull support41 | Internet ExplorerFull support4 | OperaFull support35 | SafariFull support1.3 | WebView AndroidFull support48 | Chrome AndroidFull support48 | Firefox for AndroidFull support41 | Opera AndroidFull support35 | Safari on iOSFull support1 | Samsung InternetFull support5.0 | DenoFull support1.0 | Node.jsFull support6.0.0 |
Legend
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.