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

  

猜你喜欢

转载自www.cnblogs.com/fanbi/p/9267299.html