在 jscript regexp 字符类中转义正斜杠?
我有一个针对数字(0-9
)和/或正斜杠(/
)的正则表达式测试。它看起来像这样:
/^[0-9/]+$/i.test(value)
现在我相信这是正确的,但 eclipse javascript 验证器不同意:
标记“]”存在语法错误,请删除此标记
我想这是因为分隔符/定界符是 /
并且 eclipse“认为”正则表达式已完成(因此 ]
会出乎意料)。
我们可以通过转义 /
来满足 Eclipse 的要求,如下所示:
/^[0-9\/]+$/i.test(value)
请注意,这两个版本都适合我。
我的问题是:
- 据我所知,我不需要专门在该范围内转义正斜杠。它可能是特定于情况的(例如,对于 javascript,它是使用的分隔符)。
- 虽然它们似乎都可以工作,但由于不同环境中的行为,我宁愿使用“正确”版本,而且,好吧..因为正确和全部:)
有人知道我应该做什么吗? 是否转义? 我没有找到任何有信誉的网站告诉我在一定范围内转义 /
,但 Eclipse 验证器可能并不完全愚蠢......
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:
- 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 :)
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该标准明确表示您可以将任何未转义的内容放入字符类中,除了
\
、]
和换行符:(http://es5.github.com/#x7.8.5 )。无需转义
/
。另一方面,我个人在有疑问时会逃避一切,只是为了让不太聪明的解析器高兴。
The standard clearly says you can put anything unescaped in a character class except
\
,]
and newline:( 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.