2009-11-02 33 views
0

在给定的代码中,best.test(password)返回true,但是当我在if() condition中使用它时,它将其视为false。使用正则表达式,如果和与.test()函数冲突

代码:

if(best.test(password))    //It takes it as a false . 
{ 
    document.write(best.test(password)); 
    tdPwdStrength.innerHTML="best"+best.test(password); //but in actual it is true and returning true. 
}              

请推荐!

+2

您将需要添加'test()'代码# – 2009-11-02 19:57:07

+0

否如果最好是正则表达式 – Victor 2009-11-02 20:14:04

回答

1

什么是best?它是一个'全球'RegExp,即与g标志设置?

如果是这样,那么每次调用testexec你会得到不同的答案,因为它会记住有以前的字符串的索引和搜索:

var r= /a/g;    // or new RegExp('a', 'g') 
alert(r.test('aardvark')); // true. matches first `a` 
alert(r.test('aardvark')); // true. matches second `a` 
alert(r.test('aardvark')); // true. matches third `a` 
alert(r.test('aardvark')); // false! no more matches found 
alert(r.test('aardvark')); // true. back to the first `a` again 

JavaScript的正则表达式接口是完全混乱的小陷阱喜欢这个。小心。