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

HTML/XHTML教程
Xhtml第11天:如何制作不用表格的菜单
Xhtml第3天:定义语言编码
Xhtml第2天:什么是名字空间
CSS滤镜之Wave属性
CSS滤镜之Chroma属性
(译)用CSS设计日历
CSS滤镜之Shadow属性
CSS滤镜之Glow属性
CSS滤镜之Mask属性
在网页中实现细线边框的两种方法
背景音乐是如何插入的?
CSS 标签属性/属性参考
弹出网页窗口全攻略(html/hta)
五彩缤纷建网页(1)
五彩缤纷建网页(2)色彩的基本知识
五彩缤纷建网页(4)背景与字体的搭配经验
五彩缤纷建网页(3)色彩的象征
精通 CSS 滤镜(四)
精通 CSS 滤镜(三)
XHTML代码规范

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-04   浏览: 73 ::
收藏到网摘: 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(