简体   繁体   English

正则表达式:匹配hh:mm,hh.mm或h

[英]Regular Expression: match hh:mm, hh.mm or h

I'm looking for a regular expression to match different time formats (for a time tracking web-app) in javascript . 我正在寻找一个正则表达式来匹配javascript中不同的时间格式(用于时间跟踪网络应用)。 The user can insert times with one of the following formats: 用户可以使用以下格式之一插入时间:

h
hh:mm
hh.mm

I can easily create a regular expression to match hh:mm and hh.mm, but i can't get the single hour format working. 我可以轻松创建一个正则表达式来匹配hh:mm和hh.mm,但是我无法使用单个小时格式。

This is my current regex: ([0-2][0-9])(.|:)([0-5][0-9]) 这是我当前的正则表达式: ([0-2][0-9])(.|:)([0-5][0-9])

Allowed character: 0-9 , . 允许的字符: 0-9. and : . 并且: If the user types any other character, the validation should fail. 如果用户键入任何其他字符,则验证将失败。

Does anyone have any suggestions? 有没有人有什么建议?

Edit 编辑

following formats should work to: 以下格式应适用于:

h:mm (3:30) h:mm(3:30)

solution: http://regexr.com?31gc3 解决方案: http//regexr.com?31gc3

You can make a block optional by placing it in ( ... )? 您可以通过将块放置在( ... )?来使其成为可选块( ... )? This is equivalent to ( ... ){0,1} which allows zero or one references. 这等效于( ... ){0,1} ,它允许零个或一个引用。

Your expression becomes: 您的表情变为:

/([0-2][0-9])((.|:)([0-5][0-9]))?/

This matches 12 , 12:30 and 12.30 . 这匹配1212:3012.30 It won't match 5 , 5:30 , or 5.30 . 它不会匹配5 : 5:305.30 Enabling a single digit hour input can be done by making the first digit optional: 可以通过将第一位数字设为可选来启用一位数字小时输入:

/([0-2]?[0-9])((.|:)([0-5][0-9]))?/

If you're using .match , you will notice you have 5 results: 如果您使用.match ,则会发现有5个结果:

["12:30", "12", ":30", ":", "30"]

You can reduce that to 3 by eliminating unnecessary matching when you turn ( ... ) into (?: ... ) ( ... )转换为(?: ... )时,可以通过消除不必要的匹配将其减少到3

/([0-2]?[0-9])(?:(?:.|:)([0-5][0-9]))?/

This gives you: 这给你:

["12:30", "12", "30"]

Update 更新

Based on your update, you want to match boundaries. 根据您的更新,您想匹配边界。 There are a couple ways to do this. 有几种方法可以做到这一点。

  1. Starting with ^ will tie the front of your expression to the beginning of each line/string. ^开头会将表达式的开头与每个行/字符串的开头联系起来。
  2. Ending with $ will tie the end of your expression to the end of the string. $结尾将表达式的末尾与字符串的末尾联系起来。
  3. Starting or ending with \\b will mandate that the edge is against a "boundary". \\b开头或结尾将要求该边缘违反“边界”。

Putting that all together: 把它们放在一起:

If you just want to match lines that contain nothing but the date you can use: 如果您只想匹配仅包含日期的行,则可以使用:

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

This will not catch "hello 1.30" or "1.30 hello". 这不会捕获“ hello 1.30”或“ 1.30 hello”。

If you want to match lines that start with a date you could use: 如果要匹配以日期开头的行,可以使用:

/^([0-2]?[0-9])(?:(?:.|:)([0-5][0-9]))?/

But this will match "1.30000". 但这将匹配“ 1.30000”。

your best bet if you're looking for dates at the start of lines is: 如果您要在行首查找日期,那么最好的选择是:

/^([0-2]?[0-9])(?:(?:.|:)([0-5][0-9]))?\b/

As it will match "1.30 test" but not "1.300000". 因为它将匹配“ 1.30测试”,但不匹配“ 1.300000”。 Unfortunately, it will also match "1.30.30", but that is a limitation of JavaScript's RegExp processor. 不幸的是,它也将匹配“ 1.30.30”,但这是JavaScript的RegExp处理器的限制。

If you're looking for times inside strings, this becomes: 如果要在字符串中查找时间,则变为:

/\b([0-2]?[0-9])(?:(?:.|:)([0-5][0-9]))?\b/

It matches "test 1.30 test" with the unfortunate case of matching stuff like ".10.10.10". 它将“ test 1.30 test”与不幸的情况匹配,例如“ .10.10.10”。

不知道h是24还是12小时格式,但是对于24小时,它将执行工作/^([2][0-3]|[01]?[0-9])([.:][0-5][0-9])?$/并持续12小时-这个/^([1][0-2]|[0]?[0-9])([.:][0-5][0-9])?$/

If you don't need to capture anything, just use this: 如果您不需要捕获任何东西,请使用以下命令:

/[0-2]?\d[.:][0-5]\d/

See it here in action: http://regexr.com?31g9u 实际操作见这里: http//regexr.com?31g9u


If you want to capture the hours and the minutes, use this: 如果要捕获小时和分钟,请使用以下命令:

/([0-2]?\d)[.:]([0-5]\d)/

If your capturing has other requirements, please specify. 如果您的拍摄还有其他要求,请指定。


Update: I just realized that you might only want a single digit hour if no minutes are provided. 更新:我刚刚意识到,如果没有提供分钟,您可能只希望一位数小时。 If that's the case, use this: 如果是这样,请使用以下命令:

/^(?:\d|[0-2]\d[.:][0-5]\d)$/

See it here in action: http://regexr.com?31ga1 观看此处操作: http : //regexr.com?31ga1


If you want to match something like 9:42 , but also match single digits, use this: 如果要匹配9:42东西,但又要匹配9:42数字,请使用以下代码:

/^(?:\d|[0-2]?\d[.:][0-5]\d)$/

See it here in action: http://regexr.com?31ga7 观看此处操作: http : //regexr.com?31ga7

使用此正则表达式([0-2]\\d)(\\.|:)([0-5]\\d)

Use a ? 用一个? to make something optional. 使某些东西可选。 In your case, you want something like 就您而言,您想要类似

([0-1]?[0-9])(\.|:)([0-5][0-9])

Adding the ? 添加? allows it to accept something like 5:30. 允许它接受5:30之类的东西。

Edit: also, the . 编辑:另外, . stands for any character , so it needs to be escaped. 代表任何字符 ,因此需要转义。 You can also use \\d instead of [0-9] 您也可以使用\\d代替[0-9]

如果不在右括号[0-9]之间,则点必须转义\\\\ d

/(\d|[01]\d|2[0-3])([:.][0-5]\d)?/

This one matches 12 hour time formats as well as just hours. 此匹配12小时时间格式以及仅几个小时。

(([01]?[0-9]|2[0-3])[:.][0-5][0-9])|([01]?[0-9]|2[0-3])

5      Pass
5.55   Pass
01.4   FAIL
01:59  Pass
1:45   Pass

If you want 24 hour, the part before the colon is ([01]?[0-9]|2[0-3]) 如果要24小时,则冒号前的部分是([01]?[0-9]|2[0-3])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM