RegExp.prototype[Symbol.replace]()

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.replace]() 方法指定了 String.prototype.replace()String.prototype.replaceAll() 在传入作为模式的正则表达式时的行为。

尝试一下

class RegExp1 extends RegExp {
  [Symbol.replace](str) {
    return RegExp.prototype[Symbol.replace].call(this, str, "#!@?");
  }
}

console.log("football".replace(new RegExp1("foo")));
// Expected output: "#!@?tball"

语法

js
regexp[Symbol.replace](str, replacement)

参数

str

正则替换的目标字符串。

newSubStr (replacement)

类型为 String 的替换器。支持大多数特殊的替换匹配模式;见 String.prototype.replace()指定字符串作为替换项部分。

function (replacement)

生成新的子字符串的回调函数替换器。作用于该函数的参数的详细描述见 String.prototype.replace()指定函数作为替换项部分。

返回值

用替换器替换相应匹配项后的新字符串。

描述

如果匹配模式也是 RegExp 对象,这个方法在 String.prototype.replace() 的内部调用。例如,下面的两个方法返回相同结果。

js
"abc".replace(/a/, "A");

/a/[Symbol.replace]("abc", "A");

该方法是为了在 RegExp 子类中自定义匹配的替换模式。

如果匹配模式不是一个RegExp 对象,String.prototype.replace() 就不会调用该方法,也不会创建一个 RegExp对象。

示例

直接调用

这个方法基本可以和 String.prototype.replace() 一样使用,不同之处是 this 和参数顺序。

js
var re = /-/g;
var str = "2016-01-01";
var newstr = re[Symbol.replace](str, ".");
console.log(newstr); // 2016.01.01

在子类中使用 [Symbol.replace]()

RegExp 的子类可以覆写 [Symbol.replace]() 方法来修改默认行为。

js
class MyRegExp extends RegExp {
  constructor(pattern, flags, count) {
    super(pattern, flags);
    this.count = count;
  }
  [Symbol.replace](str, replacement) {
    // 执行 [Symbol.replace]() |count| 次。
    var result = str;
    for (var i = 0; i < this.count; i++) {
      result = RegExp.prototype[Symbol.replace].call(this, result, replacement);
    }
    return result;
  }
}

var re = new MyRegExp("\\d", "", 3);
var str = "01234567";
var newstr = str.replace(re, "#"); // String.prototype.replace 调用 re[Symbol.replace]()。
console.log(newstr); // ###34567

规范

Specification
ECMAScript® 2025 Language Specification
# sec-regexp.prototype-%symbol.replace%

浏览器兼容性

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.replace]

Legend

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

Full support
Full support

参见