日期:2014-05-16  浏览次数:20400 次

自己写js replaceAll

在js里实现类似java的replaceAll有俩中途径

1.为string添加ReplaceAll方法

?

String.prototype.ReplaceAll = function (AFindText,ARepText){
raRegExp = new RegExp(AFindText,"g")
return this.replace(raRegExp,ARepText)
};
alert("s|df|s|f".ReplaceAll("\\|","->")+"\n"+"s|df|s|f");

?

2.或者更简单一点

alert("s|df|s|f".replace(/\|/gm,"->")+"\n"+"s|df|s|f")

?

这在替换文本,或者高亮显示很方便的

?

?