javascript正则表达式为00-59(秒)之间的数字 [英] javascript regexp for numbers between 00-59 (seconds)
问题描述
我想检查字段是否是有效时间值(仅为秒)。所以我想接受0到59之间的数字。我出来了:
I want to check if a field is a valid time value (just seconds). So I want to accept the numbers from 0 to 59. I came out with this:
[0-5][0-9]?
几乎完成了这项工作。但不包括数字7-8-9 ...如果用户数字07,它可以工作,但我不想强迫用户数字前0.这样我尝试了这样的事情:
which almost does the job. But excludes the digits 7-8-9... It works if the user digit 07, but I don't want to force the user to digit the first 0. So I tried something like this:
([0-5][0-9]? | [0-9])
但是这不起作用并产生太多递归调用的错误。
but this does not works and produces an error of too many recursive calls.
任何想法?
推荐答案
在你的第二个正则表达式中,你需要从第一个删除?
部分,并使其 [1-5]
而不是 [0-5]
:
In your 2nd regex, you need to remove that ?
from the first part, and make it [1-5]
instead of [0-5]
:
[0-9]|[1-5][0-9]
如果你想足够灵活,允许 7
和 07
,然后使用 [0-5]
:
And if you want to be flexible enough to allow both 7
and 07
, then use [0-5]
:
[0-9]|[0-5][0-9]
然后,简化上述内容正则表达式,你可以使用:
And then, simplifying the above regex, you can use:
[0-5]?[0-9] // ? makes [0-5] part optional
这篇关于javascript正则表达式为00-59(秒)之间的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!