Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29958756
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: 系统运维

2008-04-07 15:20:36

第一次发现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!
阅读(1684) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~