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

Javascript
js支持158、159开头的手机号的验证
javascript学习网址备忘
JScript的条件编译
[原创]站长必须要知道的javascript广告代码
js实现的点击超链显示隐藏层
JCalendar 日历控件 v1.0 beta[兼容IE&Firefox] 有文档和例子
JavaScript 不只是脚本
把JS与CSS写在同一个文件里的书写方法
Javascript & DHTML 实例编程(教程)基础知识
Javascript & DHTML 实例编程(教程)DOM基础和基本API
javascript实现的一个图片转移效果
用js实现键盘方向键翻页功能的代码
js实现DIV的一些简单控制
用CSS+JS实现的进度条效果效果
爱恋千雪-US-AscII加密解密工具(网页加密)下载
用javascript实现始终保持打开同一个子窗口以及关闭父窗口同时自动关闭所有子窗口
非常不错的页面特效 建议大家看下
javascript Zifa FormValid 0.1表单验证 代码打包下载
实现javascript的延期执行或者重复执行的两个函数
推荐一个不错的图片浏览效果

Javascript 中的 JavaScript 中的replace方法说明


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