문제

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

도움이 되었습니까?

해결책

Not sure if h is in 24 or 12 hours format but for 24 hour this will do the job /^([2][0-3]|[01]?[0-9])([.:][0-5][0-9])?$/ and for 12 hour - this /^([1][0-2]|[0]?[0-9])([.:][0-5][0-9])?$/

다른 팁

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

use this regex ([0-2]\d)(\.|:)([0-5]\d)

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

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]

the dot must be escaped if not between right brackets [0-9] is equivalent to \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])

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top