非常教程

JavaScript参考手册

RegExp

regExp.source

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

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

|:----|

| Writable | no |

| Enumerable | no |

| Configurable | yes |

示例

使用source

var regex = /fooBar/ig;

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

清空正则表达式并转义

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

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)

?

?

?

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络