admin管理员组文章数量:1406178
Hi i'm working on some regex now and i want to add the conditional to allow only numbers greater than 0.5 here is my regex
^(?![.0]*$)[0-9]+(?:\.[1-9]{1,2})?$
i just want to values between 0 and 0.5 don't match this. Thanks
Hi i'm working on some regex now and i want to add the conditional to allow only numbers greater than 0.5 here is my regex
^(?![.0]*$)[0-9]+(?:\.[1-9]{1,2})?$
i just want to values between 0 and 0.5 don't match this. Thanks
Share Improve this question asked Mar 31, 2015 at 22:37 quiquequique 1291 gold badge2 silver badges10 bronze badges 3- 4 why wouldn't you just convert to number and pare that way? – austin wernli Commented Mar 31, 2015 at 22:38
- 5 ?? Why on earth wouldn't you just convert the string to a number and do a numeric parison?? – Pointy Commented Mar 31, 2015 at 22:38
- Because i need a regex thats the way you can learn something i know i can make a numeric parison i asked for a regex – quique Commented Apr 1, 2015 at 0:54
5 Answers
Reset to default 8Regular expressions are awesome, but they can get hard to read and maintain. This feels like a scenario where you should just parse the string and pare the value.
var num = parseFloat(input);
if (num > 0.5)
...
(^(?![.0]*$)[1-9]+(?:\.[0-9]{1,2})?$)|(^(?![.0]*$)[0]+(?:\.[5-9][0-9]*)?$)
and its easy to read too!
This regex works allows two decimals and numbers greater than 0.50
^((?!0*(\.0+)?$)[0-9]+|[0-9]+[0-9]\.[0-9]{1,2}+|[1-9]\.[0-9]+|0\.[5-9][0-9]?)$
You really should use @dontangg 's answer.
But if you want a regex, here is one that do the job:
^(?:0\.5[1-9]\d*|0\.[6-9]\d*|\d+[1-9](?:\.\d+)?)$
Explanation:
^ : 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
It matches:
0.51
12
12.34
It doesn't match:
0
0.2
0.25
0.5
0.50
This regexp may works: (Check here)
^([0-9]+|[0-9]+[0-9]\.[0-9]+|[1-9]\.[0-9]+|0\.[5-9][0-9]*)$
本文标签: javascriptRegex to allow number greater than 05Stack Overflow
版权声明:本文标题:javascript - Regex to allow number greater than 0.5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744971456a2635258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论