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.
You can try the following
It can detect the following patterns :
Your original regular expression has flaws: it wouldn't match
04:00
for example.This may work better:
The best would be for HH:MM without taking any risk.
A slight modification to Manish M Demblani's contribution above handles 4am (I got rid of the seconds section as I don't need it in my application)
handles: 4am 4 am 4:00 4:00am 4:00 pm 4.30 am etc..
As you asked the left most bit optional, I have done left most and right most bit optional too, check it out
It matches with
The live link is available here
The below regex will help to validate hh:mm format