在jscript regexp字符类中转发斜杠? [英] Escape forward slash in jscript regexp character class?
问题描述
我有正式表达式测试数字( 0-9
)和/或转斜线( /
) 。看起来像这样:
I have a regular expression testing for numbers(0-9
) and/or forward slashes (/
). It looks like this:
/^[0-9/]+$/i.test(value)
现在我相信这是正确的,但eclipse JavaScript验证器不同意:
Now I believe this to be correct, but the eclipse javascript validator disagrees:
令牌]上的语法错误,删除此令牌
Syntax error on token "]", delete this token
我想这是因为分隔符/分隔符是 /
,而eclipse'认为'正则表达式已经完成(因此一个]
将是意外的)。
I suppose this is because the separator/delimiter is /
and eclipse 'thinks' the regex is finished (and therefore a ]
would be unexpected).
我们可以通过转义 /
来满足eclipse:
We can satisfy eclipse by escaping the /
like so:
/^[0-9\/]+$/i.test(value)
请注意,两个版本都适用于我。
Note that both versions work for me.
我的问题是:
- 据我所知,我不要需要在该范围内专门转义正斜杠。这可能是具体的情况(对于javascript,是使用的分隔符)。
- 虽然它们似乎都工作,但我宁愿使用正确的版本,因为行为在不同的环境中,以及..因为正确和全部:)
- As far as I know I do not need to escape the forward slash specifically in that range. It might be situation specific (as in, for javascript it is the used delimiter).
- Although they both appear to be working, I'd rather use the 'correct' version because of behaviour in different environments, and, well.. because correct and all :)
有谁知道我应该做什么? 逃避与否?我没有找到任何有信誉的网站,告诉我逃避一个范围内的 /
,但Eclipse验证器可能不是完全愚蠢的...
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 )。不需要逃避 /
。
另一方面,我个人会毫不怀疑地逃避一切,只是为了使不太聪明的解析器快乐。
On the other side, I personally would escape everything when in doubt, just to make less smart parsers happy.
这篇关于在jscript regexp字符类中转发斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!