动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   网页设计 → 阅读文章

 JavaScript 中的replace方法

作者:郁郁小蝎 来源:中国站长学院 
阅读 2315 人次 , 2006-2-15 14:46:00 

1第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符. 
 2而str.replace(/\-/g,"!")则可以替换掉全部匹配的字符(g为全局标志)。
 3
 4
 5replace() 
 6The replace() method returns the string that results when you replace text matching its first argument 
 7(a regular expression) with the text of the second argument (a string). 
 8If the g (global) flag is not set in the regular expression declaration, this method replaces only the first 
 9occurrence of the pattern. For example, 
10
11var s = "Hello. Regexps are fun.";s = s.replace(/\./"!"); // replace first period with an exclamation pointalert(s); 
12
13produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to 
14perform a global replace, finding and replacing every matching substring. For example, 
15
16var s = "Hello. Regexps are fun.";s = s.replace(/\./g, "!"); // replace all periods with exclamation pointsalert(s); 
17
18yields this result: “Hello! Regexps are fun!”    
 本文Tags组网  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:网页经典代码(六)
· 下一篇:我有我主张!随心所欲的定制“弹出窗口”
· 准备选举自己最喜欢的开发工具
· Javascript实例教程(3-1)
· [Dreamweaver教程]Meta标签详解
· Web界面设计新趋势
· 什么是符合web标准的网站


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077