Reputation: 227220
I was answering a question about regexes, when I noticed something weird. I was testing regexes in RegexPal, and I was getting different results from what Chrome and Firefox were telling me.
The regex in question is: /^\d*(\.\d{1,2}$)?/
.
In Firefox and Chrome: /^\d*(\.\d{1,2}$)?/.test('-1')
returns true
, but RegexPal says it doesn't match.
http://regexpal.com/?flags=m®ex=%5E%5Cd%2A(%5C.%5Cd%7B1%2C2%7D%24)%3F&input=-1
My guess is that it has something to do with the fact that the regex matches nothing, by which I mean the empty string.
The regex matches the start of the string, then 0 numbers, then 0 of the group, so it would "match" -1
, as it fits that mold, but the "match" would be "nothing" (empty string).
Why does RegexPal say it doesn't match, but my web browsers say it does?
Upvotes: 1
Views: 327
Reputation: 227220
RegexPal is working, but it's just not obvious. The regex matches the empty string, so RegexPal highlights that for you.
Well, since the empty string is empty, you can't see that it's highlighting it.
Upvotes: 1