一般使用replace
let str = "2018-8-14"; str.replace('-','/')//2018/8-14
並沒有替換第二個”-“,
所以我們用正則表達式重寫一個
String.prototype.myReplace =
String.prototype.myReplace ||function(oldStr, newStr){//oldStr替換成newStr
let reg = new RegExp(oldStr, "g"); //創建正則RegExp對象
return this.replace(reg, newStr);
}
str.myReplace('-','/');//2018/8/14