Use JavaScript to achieve global substitution, solve the problem of special symbols such as $ [_ Regular expression

Source: Internet
Author: User
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.