Why this match fail?
I have this code :
var tlTe开发者_JAVA百科mp=new Array();
tlTemp.push("00 - 01:??:?? - TL 1");
tlTemp.push("00 - 12:??:?? - TL 2");
for(i=0; i<tlTemp.length; i++) {
var removedTL = tlTemp[i].match(/^(\d\d) - (\?\?|10|0\d):(\?\?|[0-5]\d):(\?\?|[0-5]\d) - (.*)/);
if(removedTL!=null) {
alert("ok");
}
else
{
alert("no");
return;
}
}
and I don't understand why first string print ok and the second (so similar) no. Why?
The appropriate part of the regexp that defines the different part of the string is:
(\?\?|10|0\d)
It matches:
??
10
0x where x is a digit
So 12 does not match.
Now, also there is TL 2
instead of TL 1
but in the regexp this is defined as:
(.*)
which matches everything so that is not causing the problem.
Because your regular expression explicitly excludes it.
This section:
/^(\d\d) - (\?\?|10|0\d)
constrains matches to strings starting with two digits, a space, a dash, and a space, and then either "??", "10", or "0" followed by a digit.
This part of your regular expression: (\?\?|10|0\d)
should be changed to (\?\?|10|\d\d)
. The zero is changed to a \d. in the first string, that part of the string is 01
, while the second string has 12
, not matching the regular expression.
精彩评论