首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

regExp.source

source属性返回一个值为当前正则表达式对象的模式文本的字符串,该字符串不会包含正则字面量两边的斜杠以及任何的标志字符。

| RegExp.prototype.source属性的属性特性 |

|:----|

| Writable | no |

| Enumerable | no |

| Configurable | yes |

示例

使用source

代码语言:javascript
复制
var regex = /fooBar/ig;

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

清空正则表达式并转义

从ECMAScript 5开始,该source属性不再为空的正则表达式返回一个空字符串。而是"(?:)"返回字符串。另外,行终止符(例如“\ n”)现在被转义了。

代码语言:javascript
复制
new RegExp().source; // "(?:)"

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

规范

Specification

Status

Comment

ECMAScript 3rd Edition (ECMA-262)

Standard

Initial definition. Implemented in JavaScript 1.2. JavaScript 1.5: source is a property of a RegExp instance, not the RegExp object.

ECMAScript 5.1 (ECMA-262)The definition of 'RegExp.prototype.source' in that specification.

Standard

The source property for empty regular expressions now returns "(?:)" instead of an empty string. A definition for the escaping behavior has been added.

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'RegExp.prototype.source' in that specification.

Standard

source is now a prototype accessor property rather than an instance's own data property.

ECMAScript Latest Draft (ECMA-262)The definition of 'RegExp.prototype.source' in that specification.

Draft

?

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

"(?:)" for empty regexps

No support

?

38 (38)

?

?

(Yes)

Escaping

?

?

38 (38)

?

?

?

Prototype accessor property

?

?

41 (41)

?

?

?

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

"(?:)" for empty regexps

?

?

?

38.0 (38)

?

?

?

Escaping

?

?

?

38.0 (38)

?

?

?

Prototype accessor property

?

?

?

41.0 (41)

?

?

?

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com