ES6 - RegExp replace()

此方法在替换匹配的模式后返回一个新字符串。

语法

str.replace(regexp|substr, newSubStr|function)

参数详细信息

  • Regexp − 正则表达式对象。

  • Substr − 要替换的字符串。

  • newSubStr − 替换字符串。

  • function − 创建新字符串的函数。

返回值

替换所有匹配项后的新字符串。

示例

var str = 'Good Morning'; 
var newstr = str.replace('Morning', 'Night'); 
console.log(newstr);  

输出

Good Night