用JavaScript实现全局替换,解决$等特殊符号的难题[
程序员文章站
2022-06-22 14:33:16
感谢海浪提供的正则,原贴请参见: http://www.iecn.net/bbs/view/106503.html 因为要做个模板替换的东西,里面的变量采用${myname...
感谢海浪提供的正则,原贴请参见:
http://www.iecn.net/bbs/view/106503.html
因为要做个模板替换的东西,里面的变量采用${myname}这种格式的命名方式。在进行全局替换时,遇到两个难点:
1.要么无法替换掉$等特殊符号
2.要么无法忽略大小写
在海浪有帮助下,终于有了最佳实现方式:)
最佳实现方式:
以前使用方法一:(可实现忽略大小,但无法实现特殊符号的替换)
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>
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>
以前使用的方式二:(可替换特殊符号$等,但无法忽略大小写) 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>
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>
上一篇: Python正则表达式匹配字符串中的数字
下一篇: linux中的常用命令与快捷键介绍