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

Javascript
关于setEndPoint msdn给出的参考
一个不用onmouseup的拖动函数
实现163邮箱的图标功能
javascript 也来玩玩图片预加载
又一个小巧的图片预加载类
使用正则替换变量
JS统计Flash被网友点击过的代码
JS脚本混淆、加密讨论
通过Unicode转义序列来加密,按你说的可以算是混淆吧
javascript之ESC(第二类混淆)
另一种希望别人无法修改js的代码
javascript又一解密过程,推荐的,会这个基本上好多都能解决了
ESC之ESC.wsf可以实现javascript的代码压缩附使用方法
javascript支持区号输入的省市二级联动下拉菜单
一个不错的渐显菜单
javascript之水平横向滚动歌词同步的应用
javascript实现表现、结构、行为分离的选项卡效果!
[全兼容哦]--实用、简洁、炫酷的页面转入效果loing
用js判断浏览器是否是IE的比较好的办法
延时重复执行函数 lLoopRun.js

Javascript 中的 JavaScript 中的replace方法说明


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