Web/JavaScript/Reference/Global objects/Symbol/replace

From Get docs


The Symbol.replace well-known symbol specifies the method that replaces matched substrings of a string. This function is called by the String.prototype.replace() method.

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


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


Examples

Using Symbol.replace

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

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

Specifications

Specification
ECMAScript (ECMA-262)The definition of 'Symbol.replace' in that specification.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
replace Chrome

Full support 50

Edge

Full support 79

Firefox

Full support 49

IE

No support No

Opera

Full support 37

Safari

Full support 10

WebView Android

Full support 50

Chrome Android

Full support 50

Firefox Android

Full support 49

Opera Android

Full support 37

Safari iOS

Full support 10

Samsung Internet Android

Full support 5.0

nodejs

Full support 6.0.0

Legend

Full support  
Full support
No support  
No support


See also