Javascript RegEx:如何匹配所有大于 49 的数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8592488/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:37:59  来源:igfitidea点击:

RegEx: How can I match all numbers greater than 49?

javascriptregex

提问by Maxx

I'm somewhat new to regular expressions and am writing validation for a quantity field where regular expressions needto be used.

我对正则表达式有点陌生,正在为需要使用正则表达式的数量字段编写验证。

How can I match all numbers greater than or equal to 50?

如何匹配所有大于或等于 50 的数字?

I tried

我试过

[5-9][0-9]+

but that only matches 50-99. Is there a simple way to match all possible numbers greater than 49? (only integers are used)

但这只能匹配 50-99。有没有一种简单的方法来匹配所有可能大于 49 的数字?(仅使用整数)

回答by pimvdb

The fact that the first digit has to be in the range 5-9only applies in case of two digits. So, check for that in the case of 2 digits, and allow any more digits directly:

第一个数字必须在范围内的事实5-9仅适用于两位数字的情况。因此,在 2 位数的情况下检查,并直接允许更多位数:

^([5-9]\d|\d{3,})$

This regexp has beginning/ending anchors to make sure you're checking all digits, and the string actually represents a number. The |means "or", so either [5-9]\dor any number with 3 or more digits. \dis simply a shortcut for [0-9].

这个正则表达式有开始/结束锚点,以确保您检查所有数字,并且字符串实际上代表一个数字。的|装置“或”,所以任一[5-9]\d或任何数量的具有3个或多个数字。\d只是 的捷径[0-9]

Edit:To disallow numbers like 001:

编辑:禁止数字,如001

^([5-9]\d|[1-9]\d{2,})$

This forces the first digit to be not a zero in the case of 3 or more digits.

在 3 位或更多位的情况下,这会强制第一个数字不是零。

回答by Tiago

I know there is already a good answer posted, but it won't allow leading zeros. And I don't have enough reputation to leave a comment, so... Here's my solution allowing leading zeros:

我知道已经发布了一个很好的答案,但它不允许前导零。而且我没有足够的声誉发表评论,所以......这是我的解决方案,允许前导零:

First I match the numbers 50 through 99 (with possible leading zeros):

首先我匹配数字 50 到 99(可能有前导零):

0*[5-9]\d

Then match numbers of 100 and above (also with leading zeros):

然后匹配 100 及以上的数字(也带有前导零):

0*[1-9]\d{2,}

Add them together with an "or" and wrap it up to match the whole sentence:

将它们与“或”一起添加并包裹起来以匹配整个句子:

^0*([1-9]\d{2,}|[5-9]\d)$

That's it!

就是这样!

回答by maerics

Try a conditional group matching 50-99or any string of three or more digits:

尝试条件组匹配50-99或三个或更多数字的任何字符串:

var r = /^(?:[5-9]\d|\d{3,})$/

回答by Dzmitry Lahoda

Next matchesall greater or equal to 11100:

Next 匹配所有大于或等于11100

^([1-9][1-9][1-9]\d{2}\d*|[1-9][2-9]\d{3}\d*|[2-9]\d{4}\d*|\d{6}\d*)$

For greater or equal50:

对于大于或等于50

^([5-9]\d{1}\d*|\d{3}\d*)$

See pattern and modify to any number. Also it would be great to find some recursive forward/backward operators for large numbers.

查看模式并修改为任何数字。此外,为大数找到一些递归向前/向后运算符也会很棒。

回答by PauGNU

I know this is old, but none of these expressions worked for me (maybe it's because I'm on PHP). The following expression worked fine to validate that a number is higher than 49:

我知道这很旧,但这些表达式都不适合我(也许是因为我使用的是 PHP)。以下表达式可以很好地验证数字是否大于 49:

/([5-9][0-9])|([1-9]\d{3}\d*)/

回答by Kirill Polishchuk

Try this regex:

试试这个正则表达式:

[5-9]\d+|\d{3,}