实现js的replaceAll方法

来源:互联网 发布:python二分查找 编辑:程序博客网 时间:2024/05/05 23:49
js不提供replaceAll方法,要用正规表达式实现 

第一种:

String.prototype.replaceAll  = function(s1,s2){      

        return this.replace(new RegExp(s1,"gm"),s2);     

 }    

第二种:

str =str.replace(/&/g,"@");//将str串中的&替换成@ 



转载自:http://www.csdn.net/article/a/2009-12-08/258255

0 0