ES6 - RegExp replace() 方法



此方法替換匹配的模式後返回一個新的字串。

語法

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

引數詳情

  • 正則表示式 − 正則表示式物件。

  • 子串 − 要替換的字串。

  • 新子串 − 替換字串。

  • 函式 − 用於建立新字串的函式。

返回值

替換所有匹配項後的新字串。

示例

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

輸出

Good Night     
廣告
© . All rights reserved.