[@@replace]() 方法会在一个字符串中用给定的替换器,替换所有符合正则模式的匹配项,并返回替换后的新字符串结果。用来替换的参数可以是一个字符串或是一个针对每次匹配的回调函数。

语法

regexp[Symbol.replace](str, newSubStr|function)

参数

str
正则替换的目标字符串。
newSubStr (replacement)
类型为 String 的替换器。支持大多数特殊的替换匹配模式; 见String.prototype.replace()页的Specifying a string as a parameter部分。
function (replacement)
生成新的子字符串的回调函数替换器。作用于该函数的参数的详细描述见String.prototype.replace()页的  Specifying a function as a parameter 部分。

返回值

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

描述

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

'abc'.replace(/a/, 'A');

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

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

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

示例

直接调用

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

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

在子类中使用@@replace

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

class MyRegExp extends RegExp {
  constructor(pattern, flags, count) {
    super(pattern, flags);
    this.count = count;
  }
  [Symbol.replace](str, replacement) {
    // Perform @@replace |count| times.
    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 calls re[@@replace].
console.log(newstr); // ###34567

规范

规范 状态 备注
ECMAScript 2015 (6th Edition, ECMA-262)
RegExp.prototype[@@replace]
Standard 初始定义
ECMAScript Latest Draft (ECMA-262)
RegExp.prototype[@@replace]
Draft  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
@@replaceChrome Full support YesEdge Full support YesFirefox Full support 49IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 49Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 6.0.0

Legend

Full support  
Full support
No support  
No support

另见