Symbol.replace JavaScript

The JavaScript Symbol.replace property eliminates the matched substring of a string.

Syntax:

[Symbol.replace](string)

Parameters:
Source string.

Returns:
Modified string.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>
class obj {
constructor(a) {
this.a = a; }
[Symbol.replace](string) {
return `${string}`; } }
var display =new obj("HELLO");
document.writeln(display.a);
document.writeln("WORLD!".replace(display.a));
</script>
</body>
</html>

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
class ReplaceTest {
constructor(value) {
this.value = value;
}
[Symbol.replace](string) {
return `s/${string}/${this.value}/g`;
}
}
document.write('w3schools'.replace(new ReplaceTest('jai')));
</script>
</body>
</html>