1 前言

js中字符串整体替换,只有自带的replace,并没有replaceAll,如果我们需要把字符串中的字符统一替换,可以用正则表达式,由于经常使用就在String直接加个原生方法,方便调用。

2 代码

//默认是大小写敏感
String.prototype.replaceAll=function(str,replace,ingore){
	ingore = ingore || false;
	var reg;
	if(!ingore){
		reg = new RegExp(str,"g");
	}else{
		reg = new RegExp(str,"gi");	
	}
	return this.replace(reg,replace);
}

3 例子

js用replaceAll全部替换的方法

 

  

相关文章:

  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
  • 2021-09-02
  • 2021-11-04
  • 2022-01-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
  • 2021-08-28
  • 2021-08-17
相关资源
相似解决方案