相思资源网 Design By www.200059.com
感谢海浪提供的正则,原贴请参见:
http://www.iecn.net/bbs/view/106503.html
因为要做个模板替换的东西,里面的变量采用${MyName}这种格式的命名方式。在进行全局替换时,遇到两个难点:
1.要么无法替换掉$等特殊符号
2.要么无法忽略大小写
在海浪有帮助下,终于有了最佳实现方式:)
最佳实现方式:
复制代码 代码如下:<script type="text/javascript">
String.prototype.replaceAll = stringReplaceAll;
function stringReplaceAll(AFindText,ARepText){
var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
return this.replace(raRegExp,ARepText);
}
var ssString="www.cnlei.com;www.CnLei.net;www.cnlei.org";
alert(ssString.replaceAll("cnlei","iecn"));
ssString="www.${MyName}.com;www.${MyName}.net;www.${MyName}.org";
alert(ssString.replaceAll("${MyName}","cnlei"));
ssString="www.{MyName}.com;www.{MyName}.net;www.{MyName}.org";
alert(ssString.replaceAll("{MyName}","cnlei"));
</script>
以前使用方法一:(可实现忽略大小,但无法实现特殊符号的替换)
复制代码 代码如下:<script type="text/javascript">
String.prototype.replaceString = stringReplaceAll;
function stringReplaceAll(AFindText,ARepText){
var raRegExp = new RegExp(AFindText,"ig");
return this.replace(raRegExp,ARepText);
}
var ssString="www.cnlei.com;www.CnLei.net;www.cnlei.org";
alert(ssString.replaceString("cnlei","iecn"));
ssString="www.${MyName}.com;www.${MyName}.net;www.${MyName}.org";
alert(ssString.replaceString("${MyName}","cnlei"));
ssString="www.{MyName}.com;www.{MyName}.net;www.{MyName}.org";
alert(ssString.replaceString("{MyName}","cnlei"));
</script> 以前使用的方式二:(可替换特殊符号$等,但无法忽略大小写)
复制代码 代码如下:<script type="text/javascript">
String.prototype.replaceString = function(s1,s2){
this.str=this;
if(s1.length==0)return this.str;
var idx=this.str.indexOf(s1);
while(idx>=0){
this.str=this.str.substring(0, idx)+s2+this.str.substr(idx+s1.length);
idx=this.str.indexOf(s1);
}
return this.str;
}
var ssString="www.cnlei.com;www.CnLei.net;www.cnlei.org";
alert(ssString.replaceString("cnlei","iecn"));
ssString="www.${MyName}.com;www.${MyName}.net;www.${MyName}.org";
alert(ssString.replaceString("${MyName}","cnlei"));
ssString="www.{MyName}.com;www.{MyName}.net;www.{MyName}.org";
alert(ssString.replaceString("{MyName}","cnlei"));
</script>
http://www.iecn.net/bbs/view/106503.html
因为要做个模板替换的东西,里面的变量采用${MyName}这种格式的命名方式。在进行全局替换时,遇到两个难点:
1.要么无法替换掉$等特殊符号
2.要么无法忽略大小写
在海浪有帮助下,终于有了最佳实现方式:)
最佳实现方式:
复制代码 代码如下:<script type="text/javascript">
String.prototype.replaceAll = stringReplaceAll;
function stringReplaceAll(AFindText,ARepText){
var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
return this.replace(raRegExp,ARepText);
}
var ssString="www.cnlei.com;www.CnLei.net;www.cnlei.org";
alert(ssString.replaceAll("cnlei","iecn"));
ssString="www.${MyName}.com;www.${MyName}.net;www.${MyName}.org";
alert(ssString.replaceAll("${MyName}","cnlei"));
ssString="www.{MyName}.com;www.{MyName}.net;www.{MyName}.org";
alert(ssString.replaceAll("{MyName}","cnlei"));
</script>
以前使用方法一:(可实现忽略大小,但无法实现特殊符号的替换)
复制代码 代码如下:<script type="text/javascript">
String.prototype.replaceString = stringReplaceAll;
function stringReplaceAll(AFindText,ARepText){
var raRegExp = new RegExp(AFindText,"ig");
return this.replace(raRegExp,ARepText);
}
var ssString="www.cnlei.com;www.CnLei.net;www.cnlei.org";
alert(ssString.replaceString("cnlei","iecn"));
ssString="www.${MyName}.com;www.${MyName}.net;www.${MyName}.org";
alert(ssString.replaceString("${MyName}","cnlei"));
ssString="www.{MyName}.com;www.{MyName}.net;www.{MyName}.org";
alert(ssString.replaceString("{MyName}","cnlei"));
</script> 以前使用的方式二:(可替换特殊符号$等,但无法忽略大小写)
复制代码 代码如下:<script type="text/javascript">
String.prototype.replaceString = function(s1,s2){
this.str=this;
if(s1.length==0)return this.str;
var idx=this.str.indexOf(s1);
while(idx>=0){
this.str=this.str.substring(0, idx)+s2+this.str.substr(idx+s1.length);
idx=this.str.indexOf(s1);
}
return this.str;
}
var ssString="www.cnlei.com;www.CnLei.net;www.cnlei.org";
alert(ssString.replaceString("cnlei","iecn"));
ssString="www.${MyName}.com;www.${MyName}.net;www.${MyName}.org";
alert(ssString.replaceString("${MyName}","cnlei"));
ssString="www.{MyName}.com;www.{MyName}.net;www.{MyName}.org";
alert(ssString.replaceString("{MyName}","cnlei"));
</script>
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无用JavaScript实现全局替换,解决$等特殊符号的难题[的评论...