博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

IT民工窝棚

有意思的事很多,有多少有意义的呢?
   qbq.cublog.cn
关于作者  
姓名:QBQ
职业:PG
年龄:25
位置:DL
个性介绍:有意思的事很多,有多少有意义的呢?

我的分类  




javascript 中的replace方法

第一次发现javascript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符.

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!

 发表于: 2008-04-07,修改于: 2008-04-07 15:20 已浏览103次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.0126

京ICP证041476号