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

Javascript
用javascript实现的图片马赛克后显示并切换加文字功能
通用的javascript 换行色换列色的小js
滑动门,简洁,新手上路制作篇 (小鸽子系列)
文字模糊特效
javascript实现的又一个不错的滑动导航效果
css实现的图片模糊效果
javascript跟随鼠标x,y坐标移动的字效果
js实现用于建立新的一行且增加的四个文本框为空的且被禁用
XML+XSL 与 HTML 两种方案的结合
javascript生成/解析dom的CDATA类型的字段的代码
IE与FireFox的兼容性问题分析
关于脚本操作文本域的问题
刷新时清空文本框内容的js代码
又一个不错的js浮动广告代码
网页背景渐变效果代码
非常漂亮的让背景如此暗淡(一种弹出提示信息时页面背景色调改变的方法)
[原创]jser必看的破解javascript各种加密的反向思维方法
eval(function(p,a,c,k,e,d)系列解密javascript程序
从sohu弄下来的flash中展示图片的代码
漂亮的widgets,支持换肤和后期开发新皮肤(2007-4-27已更新1.7alpha)

Javascript 中的 JavaScript 中的replace方法说明


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