欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用JavaScript實(shí)現(xiàn)全局替換,解決$等特殊符號(hào)的難題[

 更新時(shí)間:2006年12月11日 00:00:00   作者:  
感謝海浪提供的正則,原貼請(qǐng)參見:
http://www.iecn.net/bbs/view/106503.html

因?yàn)橐鰝€(gè)模板替換的東西,里面的變量采用${MyName}這種格式的命名方式。在進(jìn)行全局替換時(shí),遇到兩個(gè)難點(diǎn):
1.要么無法替換掉$等特殊符號(hào)
2.要么無法忽略大小寫

在海浪有幫助下,終于有了最佳實(shí)現(xiàn)方式:)

最佳實(shí)現(xiàn)方式:
復(fù)制代碼 代碼如下:
<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> 

以前使用方法一:(可實(shí)現(xiàn)忽略大小,但無法實(shí)現(xiàn)特殊符號(hào)的替換)
復(fù)制代碼 代碼如下:
<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> 
以前使用的方式二:(可替換特殊符號(hào)$等,但無法忽略大小寫)
復(fù)制代碼 代碼如下:
<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>

相關(guān)文章

最新評(píng)論