十进制数正则表达式,其中十进制后的数字是可选的 [英] Decimal number regular expression, where digit after decimal is optional

查看:27
本文介绍了十进制数正则表达式,其中十进制后的数字是可选的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式来验证一个数字,但不需要小数点后的数字.即.

I need a regular expression that validates a number, but doesn't require a digit after the decimal. ie.

123
123.
123.4

都是有效的

123..

无效

任何人都将不胜感激!

推荐答案

使用以下内容:

/^d*.?d*$/

  • ^ - 行首;
  • d* - 0 个或多个数字;
  • .? - 一个可选的点(转义,因为在正则表达式中,. 是一个特殊字符);
  • d* - 0 个或多个数字(小数部分);
  • $ - 行尾.
    • ^ - Beginning of the line;
    • d* - 0 or more digits;
    • .? - An optional dot (escaped, because in regex, . is a special character);
    • d* - 0 or more digits (the decimal part);
    • $ - End of the line.
    • 这允许 .5 十进制而不需要前导零,例如 0.5

      This allows for .5 decimal rather than requiring the leading zero, such as 0.5

      这篇关于十进制数正则表达式,其中十进制后的数字是可选的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆