Javascript Regex Replace

javascript-regex

// The following code is the same:
'abc'.replace(/a/, 'A');
/a/[Symbol.replace]('abc', 'A');

The [@@replace]() method replaces some or all matches of a this pattern in a 
string by a replacement, and returns the result of the replacement as a new 
string. The replacement can be a string or a function to be called for each 
match.

regexp[Symbol.replace](str, newSubStr|function)

1. str: A String that is a target of the replacement.
2. newSubStr (replacement):  The String that replaces the substring. A number 
   of special replacement patterns are supported;
3. function (replacement): A function to be invoked to create the new substring.

The replace function returns a new string with some or all matches of a pattern 
replaced by a replacement.

var re = /-/g; 
var str = '2016-01-01';
var newstr = re[Symbol.replace](str, '.');
console.log(newstr);  // 2016.01.01
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License