一般使用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

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2021-12-24
  • 2022-12-23
  • 2022-02-11
  • 2021-11-06
猜你喜欢
  • 2021-07-22
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2022-01-05
  • 2022-12-23
相关资源
相似解决方案