双数范围验证的正则表达式 [英] Regular expression for double number range validation
本文介绍了双数范围验证的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我寻找一个正则表达式来支持双数,只有一个小数部分,范围在0.1到999.9之间。
I look for a regular expression to support double number with just one fraction part which ranges between 0.1 to 999.9
这意味着不允许以下值:
It means following values are not allowed:
0
0.0 // less than lower bound
0.19 // fraction part is 2 digits, right '9' is extra
94.11 // fraction part is 2 digits, right '1' is extra
999.90 // fraction part is 2 digits, '0' is extra
9.0 // should be 9 or 9.1, 9.0 is wrong
1000 // is higher than upper bound
允许的:
1.1
55.5
999.9
我的正则表达式是:
(^(\.[1-9])?$|(^[1-9][0-9]{0,2}?(\.[1-9])?$))$
哪个不支持 0.1
到 0.9
和额外的零,如 99.000
和 99.0
Which doesn't support 0.1
to 0.9
and extra zeros like 99.000
and 99.0
测试步骤:
在浏览器控制台中:
Test steps: In your browser console:
var reg = new RegExp(/(^(\.[1-9])?$|(^[1-9][0-9]{0,2}?(\.[1-9])?$))$/);
reg.test(12);
任何帮助appriciated
Any help appriciated
推荐答案
我猜这是最准确的:
/^(0\.[1-9]|[1-9][0-9]{0,2}(\.[1-9])?)$/
请注意,在使用正则表达式文字时,您不需要 RegExp
构造函数:
Note that you don't need the RegExp
constructor when working with regex literals:
re = /^(0\.[1-9]|[1-9][0-9]{0,2}(\.[1-9])?)$/
a = [0, 0.1, 123, 123.4, '00.1', 123.45, 123456, 'foo']
a.map(function(x) { console.log(x, re.test(x)) })
0 false
0.1 true
123 true
123.4 true
00.1 false
123.45 false
123456 false
foo false
这篇关于双数范围验证的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文