javascript 最小数值为 125 的正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26001614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex for a min number value of 125?
提问by Evan
I have an input that requires a minimum number value of 125. I am new to regex, and using Foundation's Abide for validation which as far as I can tell does not have specific mix/max support. I was hoping for a regex to set in the pattern attribute. Any solutions? Thanks!
我有一个需要最小数值 125 的输入。我是 regex 的新手,并且使用 Foundation 的 Abide 进行验证,据我所知,它没有特定的混合/最大支持。我希望在模式属性中设置一个正则表达式。任何解决方案?谢谢!
回答by Luke
Any regex to comprehensively validate the value of a number is going to be beefy. I'd suggest just parsing the number to an int
and then checking it.
任何全面验证数字值的正则表达式都将是强大的。我建议只是将数字解析为 anint
然后检查它。
In Javascript:
在 JavaScript 中:
if (parseInt(numberString) >= 125)
{
// number is at least 125.
}
回答by Luke
Probably just need to cover 3 or greater digits.
可能只需要覆盖 3 位或更多位数字。
# ^[0-9]*(?:12[5-9]|1[3-9][0-9]|[2-9][0-9]{2}|[1-9][0-9]{3,})$
^
[0-9]*
(?:
12 [5-9]
|
1 [3-9] [0-9]
|
[2-9] [0-9]{2}
|
[1-9] [0-9]{3,}
)
$
回答by lavina
As per the question you want to add 125 as a minimum value, then you can handle it like
根据您要添加 125 作为最小值的问题,您可以像这样处理
/([1-9][0-9]{3,}|12[5-9]|1[3-9][0-9]|[2-9][0-9][0-9])/
Find the test cases added to this below link:
在下面的链接中找到添加到此的测试用例:
回答by Ali Murtaza Shaikh
If you have to do it through Regex , you can try this expression :
如果你必须通过 Regex 来做,你可以试试这个表达式:
([1-9][2-9][5-9])|([2-9][0-9][0-9])|([1-9]\d{3}\d*)
I have tested it in javascript and it is working fine in there :
我已经在 javascript 中测试过它,它在那里工作正常:
var pattern = /([1-9][2-9][5-9])|([2-9][0-9][0-9])|([1-9]\d{3}\d*)/;
console.log(pattern.test('120')); //false
console.log(pattern.test('125')); // true
console.log(pattern.test('200')); // true
console.log(pattern.test('10000')); // true
console.log(pattern.test('99999')); // true