RegExp[Symbol.species]

RegExp[Symbol.species] 静态访问器属性返回用于在某些 RegExp 方法中构造复制的正则表达式的构造函数。

¥The RegExp[Symbol.species] static accessor property returns the constructor used to construct copied regular expressions in certain RegExp methods.

警告:[Symbol.species] 的存在允许执行任意代码并可能产生安全漏洞。它还使某些优化变得更加困难。引擎实现者是 研究是否删除此功能。如果可能的话,避免依赖它。

¥Warning: The existence of [Symbol.species] allows execution of arbitrary code and may create security vulnerabilities. It also makes certain optimizations much harder. Engine implementers are investigating whether to remove this feature. Avoid relying on it if possible.

Try it

语法

¥Syntax

js
RegExp[Symbol.species]

返回值

¥Return value

调用 get [Symbol.species] 的构造函数 (this) 的值。返回值用于构造复制的 RegExp 实例。

¥The value of the constructor (this) on which get [Symbol.species] was called. The return value is used to construct copied RegExp instances.

描述

¥Description

[Symbol.species] 访问器属性返回 RegExp 对象的默认构造函数。子类构造函数可以重写它以更改构造函数赋值。默认实现基本上是:

¥The [Symbol.species] accessor property returns the default constructor for RegExp objects. Subclass constructors may override it to change the constructor assignment. The default implementation is basically:

js
// Hypothetical underlying implementation for illustration
class RegExp {
  static get [Symbol.species]() {
    return this;
  }
}

由于这种多态实现,派生子类的 [Symbol.species] 也会默认返回构造函数本身。

¥Because of this polymorphic implementation, [Symbol.species] of derived subclasses would also return the constructor itself by default.

js
class SubRegExp extends SubRegExp {}
SubRegExp[Symbol.species] === SubRegExp; // true

某些 RegExp 方法会在运行 exec() 之前创建当前正则表达式实例的副本,以便不会保留对 lastIndex 的更改等副作用。[Symbol.species] 属性用于确定新实例的构造函数。复制当前正则表达式实例的方法是:

¥Some RegExp methods create a copy of the current regex instance before running exec(), so that side effects such as changes to lastIndex are not retained. The [Symbol.species] property is used to determine the constructor of the new instance. The methods that copy the current regex instance are:

示例

¥Examples

普通对象中的物种

¥Species in ordinary objects

[Symbol.species] 属性返回默认构造函数,即 RegExp 对象的 RegExp 构造函数:

¥The [Symbol.species] property returns the default constructor function, which is the RegExp constructor for RegExp objects:

js
RegExp[Symbol.species]; // function RegExp()

派生对象中的物种

¥Species in derived objects

在自定义 RegExp 子类(例如 MyRegExp)的实例中,MyRegExp 种类是 MyRegExp 构造函数。但是,你可能想要覆盖它,以便在派生类方法中返回父 RegExp 对象:

¥In an instance of a custom RegExp subclass, such as MyRegExp, the MyRegExp species is the MyRegExp constructor. However, you might want to overwrite this, in order to return parent RegExp objects in your derived class methods:

js
class MyRegExp extends RegExp {
  // Overwrite MyRegExp species to the parent RegExp constructor
  static get [Symbol.species]() {
    return RegExp;
  }
}

或者你可以使用它来观察复制过程:

¥Or you can use this to observe the copying process:

js
class MyRegExp extends RegExp {
  constructor(...args) {
    console.log("Creating a new MyRegExp instance with args:", args);
    super(...args);
  }
  static get [Symbol.species]() {
    console.log("Copying MyRegExp");
    return this;
  }
  exec(value) {
    console.log("Executing with lastIndex:", this.lastIndex);
    return super.exec(value);
  }
}

Array.from("aabbccdd".matchAll(new MyRegExp("[ac]", "g")));
// Creating a new MyRegExp instance with args: [ '[ac]', 'g' ]
// Copying MyRegExp
// Creating a new MyRegExp instance with args: [ MyRegExp /[ac]/g, 'g' ]
// Executing with lastIndex: 0
// Executing with lastIndex: 1
// Executing with lastIndex: 2
// Executing with lastIndex: 5
// Executing with lastIndex: 6

规范

Specification
ECMAScript Language Specification
# sec-get-regexp-@@species

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看