第一种方式:
String.prototype.replaceAll = function(s1,s2)
{
return this.replace(new RegExp(s1,"gm"),s2);
}
第二种方式(推荐):
str1 =str1.replace(/&/g,"@");//将str1串中的&替换成@ str2 =str2.replace(/\*/g,"%");//将str2串中的*替换成%,注意转义\
本文介绍了两种在JavaScript中进行字符串替换的方法。第一种是通过扩展String.prototype实现全局替换;第二种则是直接使用replace方法,适用于特定字符的替换操作,并展示了如何正确转义特殊字符。
第一种方式:
String.prototype.replaceAll = function(s1,s2)
{
return this.replace(new RegExp(s1,"gm"),s2);
}
第二种方式(推荐):
str1 =str1.replace(/&/g,"@");//将str1串中的&替换成@ str2 =str2.replace(/\*/g,"%");//将str2串中的*替换成%,注意转义\
924
941

被折叠的 条评论
为什么被折叠?