当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript 中的replace方法说明

Javascript
javascript:以前写的xmlhttp池,代码
javascript firefox兼容ie的dom方法脚本
常用的javascript function代码
javascript Discuz代码中的msn聊天小功能
js 简单类代码
分离式javascript取当前element值的代码
javascript document.images实例
javascript 调用其他页面的js函数或变量的脚本
javascript一点特殊用法
js中cookie的使用详细分析
javascript实现通过拼音首字母快速选择下拉列表
javascript解析xml字符串的函数
用javascript和css模拟select的脚本
javascript操作select参考代码
javascript String 的扩展方法集合
javascript数组的扩展实现代码集合
宝儿的zQuery库选择器简单原型
超级经典一套鼠标控制左右滚动图片带自动翻滚
javascript关键字加亮加连接
JavaScript 应用类库代码

Javascript 中的 JavaScript 中的replace方法说明


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

第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符.
而str.replace(/\-/g,"!")则可以替换掉全部匹配的字符(g为全局标志)。
replace()
The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,
var s = "Hello. Regexps are fun.";s = s.replace(/\./, "!"); // replace first period with an exclamation pointalert(s);
produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,
var s = "Hello. Regexps are fun.";s = s.replace(/\./g, "!"); // replace all periods with exclamation pointsalert(s);
yields this result: “Hello! Regexps are fun!”