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

HTML/XHTML教程
译文:如何写出漂亮的HTML代码
Web Slices是什么
select网页下拉列表与div层遮盖问题
202个免费的高质量XHTML模板(1)
202个免费的高质量XHTML模板(2)
验证HTML,CSS以及RSS源是否正确的免费工具
网页排版应该考虑IE6的兼容性问题
表单元素与提示文字无法对齐的问题
学习如何书写整洁规范的HTML标记
网页设计潮流之2009:路在何方?
制作网页的5条非常不错的建议
Xhtml不常用却很有用的标签
Google Wave将推动HTML5标准化
是否为img图片标签赋予alt属性
iframe高度自适应代码(兼容FF,Opera,Safari)
HTML教程:small标记
WEB标准:网页页面结构
如何修改网页中的FlashSWF文件
dl,dt,dd在什么时候适合使用呢?
shtml include使用方法

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


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