Thank you for the regular of the waves, the original paste see:
Http://www.iecn.net/bbs/view/106503.html
Because you want to make a template replacement thing, inside the variable to use ${myname} This format naming method. There are two difficulties encountered when making global substitutions:
1. Or can not replace the $ and other special symbols
2. Either cannot ignore case
With the help of the waves, finally have the best way to achieve:
Best way to achieve this:
Copy Code code as follows:
<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>
Previously used method one: (can be implemented to ignore the size, but can not implement the replacement of special symbols)
Copy Code code as follows:
<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>
Previously used in the way two: (Can replace special symbol $, but cannot ignore case)
Copy code code as follows:
<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,&NBSP;IDX) +s2+this.str.substr (idx+s1.length);
idx= This.str.indexOf (S1);
}
return this.str;
}
Var ssstr Ing= "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>