轉至:https://www.cnblogs.com/jasonlam/p/7070604.html
var text = "餓~,23333。餓~,測試yongde"; var word = "餓~"; var newWorld = "額~~"; text = text.replace(word, newWorld); // 只能替換第一個 text = text.replace(new RegExp(word,'g'), newWorld); // 全局替換
利用 JS 的 RegExp 對象,將 g 參數單拿了出來,同時,正則的內容可以用變量來代替了!