发表新主题 回复该帖子
主题:JavaScript 中使用replace达到 replaceAll的效果
唧唧
帖子档案  楼主 JavaScript 中使用replace达到 replaceAll的效果   Post by : 2010-10-11 12:16:17.0
  • 幼儿园
  • 幼儿园
  • UID:3
  • 主题:342
  • 帖子:781
  • 加为好友 加为好友    发送短信 发送短信

js不提供replaceAll方法,要用正规表达式实现。

   xhtml代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3.  <head> 
  4.   <title> new document </title> 
  5.   <meta name="generator" content="editplus" /> 
  6.   <meta name="author" content="" /> 
  7.   <meta name="keywords" content="" /> 
  8.   <meta name="description" content="" /> 
  9.  </head> 
  10.  
  11.  <body> 
  12.   <script type="text/javascript"> 
  13.   <!-- 
  14.     alert("aSDFaSDF".replace("a","A")); 
  15.     alert("127.0.0.1".replace("\.","")); 
  16.  
  17.     String.prototype.replaceAll = function(s1,s2){ 
  18.         return this.replace(new RegExp(s1,"gm"),s2);//g=globalm=multiLine 
  19.     } 
  20.     alert("aSDFaSDF".replaceAll("a","A")); 
  21.     alert("127.0.0.1".replaceAll("\\.","")); 
  22.  
  23.     //或者 
  24.     alert("aSDFaSDF".replace(/a/g,"A"));     
  25.     alert("127.0.0.1".replace(/\./g,"")); 
  26.   //--> 
  27.   </script> 
  28.  </body> 
  29. </html> 
签名
 ★★★★★★★★
 纵里寻她千百度,蓦然回首,那人却在,灯火阑珊处!
 MyBlog :http://blog.javawind.net
返回页面顶部  

irene123
2F Re:~   Post by : 2011-12-07 15:52:48.0
  • 幼儿园
  • 幼儿园
  • UID:802
  • 主题:0
  • 帖子:31
  • 加为好友 加为好友    发送短信 发送短信
看得头晕了em06em06
签名
sf123
cqsf
返回页面顶部  


CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 31 ms,0 (Queries)  Gzip enabled

WAP - 清除Cookies - 联系我们 - JavaWind.Net Studio - Archiver - TOP Valid XHTML 1.0 Transitional Valid CSS! 粤ICP备07511478号