简体   繁体   中英

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 . 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.

This is my current regex: ([0-2][0-9])(.|:)([0-5][0-9])

Allowed character: 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)

solution: 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.

Your expression becomes:

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

This matches 12 , 12:30 and 12.30 . It won't match 5 , 5:30 , or 5.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:

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

You can reduce that to 3 by eliminating unnecessary matching when you turn ( ... ) into (?: ... )

/([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".

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".

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".

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". Unfortunately, it will also match "1.30.30", but that is a limitation of JavaScript's RegExp processor.

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".

不知道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


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


If you want to match something like 9:42 , but also match single digits, use this:

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

See it here in action: 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.

Edit: also, the . stands for any character , so it needs to be escaped. You can also use \\d instead of [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.

(([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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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