Regular expression for matching HH:MM time format

I want a regexp for matching time in HH:MM format. Here’s what I have, and it works:

^[0-2][0-3]:[0-5][0-9]$

This matches everything from 00:00 to 23:59.

However, I want to change it so 0:00 and 1:00, etc are also matched as well as 00:00 and 01:30. I.e to make the leftmost digit optional, to match HH:MM as well as H:MM.

Any ideas how to make that change? I need this to work in javascript as well as php.

21 Answers
21

Leave a Comment