[英]Regex to allow number greater than 0.5
嗨,我现在正在处理一些正则表达式,我想添加条件以只允许大于0.5的数字在这里,这是我的正则表达式
^(?![.0]*$)[0-9]+(?:\.[1-9]{1,2})?$
我只想0到0.5之间的值与此不匹配。 谢谢
正则表达式很棒,但是它们很难阅读和维护。 感觉就像是一个场景,您应该只分析字符串并比较值。
var num = parseFloat(input);
if (num > 0.5)
...
(^(?![.0]*$)[1-9]+(?:\.[0-9]{1,2})?$)|(^(?![.0]*$)[0]+(?:\.[5-9][0-9]*)?$)
它也很容易阅读!
此正则表达式可能有效:( 在此处检查 )
^([0-9]+|[0-9]+[0-9]\.[0-9]+|[1-9]\.[0-9]+|0\.[5-9][0-9]*)$
此正则表达式的作品允许两个小数和大于0.50的数字
^((?!0*(\.0+)?$)[0-9]+|[0-9]+[0-9]\.[0-9]{1,2}+|[1-9]\.[0-9]+|0\.[5-9][0-9]?)$
您确实应该使用@dontangg的答案。
但是,如果您要使用正则表达式,可以使用以下命令:
^(?:0\.5[1-9]\d*|0\.[6-9]\d*|\d+[1-9](?:\.\d+)?)$
说明:
^ : begining of string
(?: : begining of non-capture group
0\.5[1-9]\d*: 0. followed by number greater than 50
| :
0\.[6-9]\d* : 0. followed by number greater than 5 then any number of digits
| : OR
\d+[1-9] : any digit followed by number from 1 to 9
(?: : begining of non-capture group
\.\d+ : a dot followed by any digits
)? : end of non capture group, optional
) : end of non-capture group
$ : end of string
它匹配:
0.51
12
12.34
不匹配:
0
0.2
0.25
0.5
0.50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.