240
世界上有10种人,一种懂二进制,另一种不懂二进制。

js用replaceAll全部替换的方法

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 例子

 

  

posted @ 2018-07-05 11:12  unionline  阅读(1043)  评论(0编辑  收藏  举报