2009-11-02 2 views
0

주어진 코드에서 best.test(password)이 true를 반환하지만 if() 에서 이것을 사용하면 false로 간주됩니다.정규 표현식을 사용하면 .test() 함수와 if 및 else가 충돌합니다.

코드 :

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()'에 대한 코드를 추가해야합니다. –

+0

정규 표현식이 가장 좋으면 No입니다. – Victor

답변

1

best은 무엇입니까? '글로벌'RegExp입니까, g 플래그가 설정된 플래그입니까?

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 

자바 스크립트의 정규식 인터페이스는 혼란 작은 함정이 가득 : 그렇다면 거기에서 이전 문자열 색인 및 검색을 기억으로

, 각 시간 당신은, 당신이 다른 대답을 얻을 것이다 exectest 전화를 걸거나 이렇게. 조심해.

관련 문제