Symbol.replace

Symbol.replace 静态数据属性代表 众所周知的符号 @@replaceString.prototype.replace()String.prototype.replaceAll() 方法在其第一个参数上查找此符号,以查找替换当前对象匹配的子字符串的方法。

¥The Symbol.replace static data property represents the well-known symbol @@replace. The String.prototype.replace() and String.prototype.replaceAll() methods look up this symbol on their first argument for the method that replaces substrings matched by the current object.

有关详细信息,请参阅 RegExp.prototype[@@replace]()String.prototype.replace()String.prototype.replaceAll()

¥For more information, see RegExp.prototype[@@replace](), String.prototype.replace(), and String.prototype.replaceAll().

Try it

¥Value

众所周知的符号 @@replace

¥The well-known symbol @@replace.

Property attributes of Symbol.replace
Writable no
Enumerable no
Configurable no

示例

¥Examples

使用 Symbol.replace

¥Using Symbol.replace

js
class CustomReplacer {
  constructor(value) {
    this.value = value;
  }
  [Symbol.replace](string) {
    return string.replace(this.value, "#!@?");
  }
}

console.log("football".replace(new CustomReplacer("foo"))); // "#!@?tball"

规范

Specification
ECMAScript Language Specification
# sec-symbol.replace

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看