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>

Output:

HELLO WORLD!

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('w3spoint'.replace(new ReplaceTest('jai')));
 
</script>
</body>
</html>

Output:

s/w3spoint/jai/g
Please Share