Symbol.replace

O símbolo Symbol.replace é conhecido por especificar o método que substitui as substrings conrrespondentes de uma string. Essa função é chamada pelo método String.prototype.replace().

Para mais informações, veja RegExp.prototype[@@replace]() (en-US) e String.prototype.replace().

Experimente

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

Exemplos

Usando 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")));
// resultado esperado: "#!@?tball"

Especificações

Specification
ECMAScript Language Specification
# sec-symbol.replace

Compatibilidade com navegadores

BCD tables only load in the browser

Veja também