I have a regular expression testing for numbers(0-9
) and/or forward slashes (/
). It looks like this:
/^[0-9/]+$/i.test(value)
Now I believe this to be correct, but the eclipse javascript validator disagrees:
Syntax error on token "]", delete this token
I suppose this is because the separator/delimiter is /
and eclipse 'thinks' the regex is finished (and therefore a ]
would be unexpected).
We can satisfy eclipse by escaping the /
like so:
/^[0-9\/]+$/i.test(value)
Note that both versions work for me.
My problem with this is:
Does anyone know what I'm supposed to do? Escape or not? I did not find any reputable site that told me to escape the /
in a range, but the Eclipse-validator is probably not completely stupid...
The standard clearly says you can put anything unescaped in a character class except \
, ]
and newline:
RegularExpressionClassChar ::
RegularExpressionNonTerminator but not ] or \
RegularExpressionBackslashSequence
RegularExpressionNonTerminator ::
SourceCharacter but not LineTerminator
( http://es5.github.com/#x7.8.5 ). No need to escape /
.
On the other side, I personally would escape everything when in doubt, just to make less smart parsers happy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With