当前位置: 首页 > 图文教程 > 网页制作 > HTML/XHTML教程 > JavaScript 中的replace方法

HTML/XHTML教程
为什么设计师需要学习编写代码
select控件是如何设计的?
如何全面清理浮动?
js教程:传递单引号
教你制作有好的错误页面
设计一个提示用户登录错误的表单
Meta标签的X-UA-Compatible
hResume-发布简历的微格式
网页制作前端技术应该继续改革
来自W3C的消息:HTML5即将来临
网页设计师设计网页遵循的3条原则
网页减肥提速的9个技巧
让网页更有阅读性的几个网页设计技巧
HTML标记一定不要忘记关闭
在XHTML中不再使用HTML中的一些废弃元素
Web标准学习:理解结构和表现相分离
HTML5:b和i标记将被赋予真正的语义
HTML5中的新事件
html5标签
HTML是网页制作者必须要学习的

HTML/XHTML教程 中的 JavaScript 中的replace方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-04   浏览: 87 ::
收藏到网摘: n/a

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(