[英]javascript number regular expression
I've come up with this regular expression to validate a javascript number according to the specification: 我想出了这个正则表达式来根据规范验证javascript数字:
(-|\+|)(\d+\.?\d*|\.\d+)([eE](-|\+|)\d+)?
As far as I can think of, these are valid numbers in js: 据我所知,这些是js中的有效数字:
123,123.3, .3, -123, -.3, -.3e-2, -.3e+2, +.2e2... and so forth. 123,123.3,.3,-123,-。3,-。3e-2,-。3e + 2,+。2e2 ...等。
I've been trying to find a verified regular expression on the internet so that I could compare my solution but to no avail. 我一直在尝试在互联网上找到经过验证的正则表达式,以便可以比较我的解决方案,但无济于事。
Could anyone tell me if my approach is correct or give me a better solution? 谁能告诉我我的方法是正确的还是给我一个更好的解决方案?
While using isNan
is the correct way of checking numbers in JavaScript, you can also validate floating point numbers with [-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?
尽管使用
isNan
是检查JavaScript中数字的正确方法,但您也可以使用[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?
regex (taken from Regular-Expressions.info ). 正则表达式(摘自Regular-Expressions.info )。
Consider using appropriate anchors though! 考虑使用适当的锚! (
^
for string start, $
for string end). (
^
为字符串开头, $
为字符串结尾)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.