RegExp[Symbol.species]

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

RegExp[Symbol.species] 访问器属性返回 RegExp 的构造函数。

语法

js
RegExp[Symbol.species]

描述

species 访问器属性返回 RegExp 对象的默认构造函数。子类构造函数可能会覆盖它,来修改构造函数的指派。

示例

species属性返回默认构造函数,它是用于 RegExp 对象的 RegExp 构造函数:

js
RegExp[Symbol.species]; // 函数 RegExp()

在派生的正则类(也就是你自定义的正则类 MyRegExp)中,MyRegExp 的 species 是 MyRegExp 构造函数。但是,你可能希望覆盖它,以便在你的派生类方法中,返回 RegExp 父类对象:

js
class MyRegExp extends RegExp {
  // 将 MyRegExp species 覆盖为 RegExp 父类构造函数
  static get [Symbol.species]() {
    return RegExp;
  }
}

规范

Specification
ECMAScript® 2025 Language Specification
# sec-get-regexp-%symbol.species%

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
[Symbol.species]

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见