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]()
e String.prototype.replace()
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Property attributes of Symbol.replace |
|
---|---|
Writable | no |
Enumerable | no |
Configurable | no |
Exemplos
Usando Symbol.replace
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
Compatibilidade de navegador
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.