zado
小狐狸
小狐狸
  • UID23748
  • 注册日期2008-04-19
  • 最后登录2008-04-19
  • 发帖数1
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
阅读:3020回复:1

Javascipt 正则表达式问题

楼主#
更多 发布于:2008-04-19 15:40
function test(){
    var s = '2008-12-14';
	if (!/^\d{4}-\d{1,2}-\d{1,2}$/g.test(s))
	    alert(s);
}

...

<input type="button" value='test' onclick="test()" />


按理来说点button不会有提示,但是现在是点第一下没有提示,第二下就提示
反复点同样,奇数不提示,偶数提示。

这个正则表达式算通过还是不通过?

在IE下怎么点都不提示的。
wushi777
非常火狐
非常火狐
  • UID12365
  • 注册日期2006-04-17
  • 最后登录2011-04-13
  • 发帖数817
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度1点
1楼#
发布于:2008-04-19 15:40
Remove 'g' flag from your pattern.

If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property. For example, assume you have this script:

var myRe = /ab*/g;
var str = "abbcdefabh";
var myArray;
while ((myArray = myRe.exec(str)) != null)
{
  var msg = "Found " + myArray[0] + ".  ";
  msg += "Next match starts at " + myRe.lastIndex;
  print(msg);
}

This script displays the following text:

Found abb. Next match starts at 3
Found ab. Next match starts at 9
游客

返回顶部