Javascript RegEx - 时间验证 ((h)h:mm)

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

RegEx - Time Validation ((h)h:mm)

javascriptregex

提问by Joshua Enfield

/^\d{1,2}[:][0-5][0-9]$/

is what I have. this limits minutes to 00-59. It does not, however, limit hours to between 0 and 12. For similarity and uniformity I would like to do this with RegEx alone if possible.

是我所拥有的。这将分钟限制为 00-59。但是,它不会将小时数限制在 0 到 12 之间。为了相似性和一致性,如果可能的话,我想单独使用 RegEx 来做到这一点。

Further-more I would like the first digit to be optional. i.e. 09:30 accepted as well as 9:30. I played around with ranges, but something out of range is always acceptable.

此外,我希望第一个数字是可选的。即接受 09:30 和 9:30。我玩过范围,但超出范围的东西总是可以接受的。

采纳答案by Jon Snyder

/^(10|11|12|[1-9]):[0-5][0-9]$/

I don't think that you want 0:50 as a valid time either.

我也不认为您希望 0:50 作为有效时间。

回答by Trey Hunner

Assuming you are working in 12 hour time, 0 is not a valid hour and should also be excluded (as pointed out by Jon). Here is a basic solution:

假设您在 12 小时内工作,则 0 不是有效小时,也应排除在外(如 Jon 所指出的)。这是一个基本的解决方案:

/^(0?[1-9]|1[012]):[0-5][0-9]$/

A 24-hour time regex matcher that works similarly:

工作方式类似的 24 小时时间正则表达式匹配器:

/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/

回答by kennytm

The 0 - 9 and 10 - 12 cases need to be treated separately. (The two rules can be combined with |.)

0-9 和 10-12 的情况需要分开处理。(这两个规则可以与 结合使用|。)

/^(?:0?\d|1[012]):[0-5]\d$/

Here

这里

  • (?:…)is a non-capturing group
  • x|ymeans match either pattern
  • 0?\dmatches 0 - 9 or 00 - 09
  • 1[012]matches 10 - 12.
  • (?:…)是一个非捕获组
  • x|y表示匹配任一模式
  • 0?\d匹配 0 - 9 或 00 - 09
  • 1[012]匹配 10 - 12。

回答by Nikhil VJ

For folks looking for 24h format matching,

对于寻找 24 小时格式匹配的人们,

hh:mm:ss or h:mm:ss :

hh:mm:ss 或 h:mm:ss :

status = /^(2[0-3]|[0-1]?[\d]):[0-5][\d]:[0-5][\d]$/.test(timestr)

hh:mm or h:mm :

hh:mm 或 h:mm :

status = /^(2[0-3]|[0-1]?[\d]):[0-5][\d]$/.test(timestr)

This site is great for testing out: https://www.regexpal.com/

该站点非常适合进行测试:https: //www.regexpal.com/

Addedum: Explanation for completeness:

补充:完整性的解释:

  • ^: start of string, $: end of string. So we put the expression in a ^..$block to ensure there's nothing outside of our pattern.
  • (2[0-3]|[0-1]?[\d]): translates to 2[0-3]OR [0-1]?[\d]
  • 2[0-3]: 20, 21, 22, 23
  • [0-1]?[\d]: 0 or 1 or nothing (?) followed by any single digit(\d). So, this works for numbers from 0 to 19.
  • :just that character
  • [0-5][\d]: number from 00 to 59. Note: this won't tolerate single-digit in the mm place.
  • ^: 字符串的开头,$: 字符串的结尾。所以我们把表达式放在一个^..$块中,以确保我们的模式之外没有任何东西。
  • (2[0-3]|[0-1]?[\d]): 转换为2[0-3]OR[0-1]?[\d]
  • 2[0-3]: 20, 21, 22, 23
  • [0-1]?[\d]: 0 或 1 或没有 ( ?) 后跟任何单个数字 ( \d)。因此,这适用于从 0 到 19 的数字。
  • :就是那个性格
  • [0-5][\d]:从 00 到 59 的数字。注意:这不会容忍毫米位置的一位数。

回答by Andrey

Thanks!!! I use this Javascript code now:

谢谢!!!我现在使用这个 Javascript 代码:

function Check_arrival(time) { //Assuming you are working in 12 hour time, 0 is not a valid var patt = new RegExp("^(0?[1-9]|1[012]):[0-5][0-9]$"); var res = patt.test(time); return res; }

function Check_arrival(time) { //Assuming you are working in 12 hour time, 0 is not a valid var patt = new RegExp("^(0?[1-9]|1[012]):[0-5][0-9]$"); var res = patt.test(time); return res; }

res=true|false

资源=真|假

回答by Byorn

This is perfect: ^0?([0-9][0-2]?):[0-5][0-9]$

这是完美的:^0?([0-9][0-2]?):[0-5][0-9]$

Note: 12 Hr Clock Only

注意:仅限 12 小时时钟

For Times like: 0:01- 12:59

对于像这样的时间:0:01-12:59

or

或者

00:01 - 12:59

00:01 - 12:59