Regex for 24 Hour Time

A friend of mine asked for some help with a regular expression today that just needed to match 24 time that may or may not include minutes. A quick solution I had for JS was:

var regexp = /^(?:[0-1]?[0-9]|2[0-3])(?::[0-5][0-9])?$/;

If you atually wanted to capture hours and minutes you could use the following instead:

var regexp = /^([0-1]?[0-9]|2[0-3])(:[0-5][0-9])?$/;

The reason for that is the ?: at makes the grouping a non-capture group.

When you break the regexp down it's pretty simple, translated to plain English it says:

^ Line starts with

( a group

[0-1]? that MIGHT start with a 0 or 1

[0-9] followed by a number in a range from 0 to 9

| OR

2[0-3] a number from 20 to 23

)( followed by another group

:[0-5][0-9] that will be a colon then a number between 0 and 59

)? Oh by the way, the group I just described? Doesn't actually have to be there

$ End of the line

If you wanna play with the regexp you can check out this fiddle and mess with the tests, but it's pretty simple. http://jsfiddle.net/z6q8G/2/