Regular expression for matching HH:MM time format

前端 未结 19 2722
甜味超标
甜味超标 2020-11-22 17:25

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

19条回答
  •  囚心锁ツ
    2020-11-22 18:08

    As you asked the left most bit optional, I have done left most and right most bit optional too, check it out

    ^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]?$
    

    It matches with

    0:0 
    00:00
    00:0 
    0:00
    23:59
    01:00
    00:59
    

    The live link is available here

提交回复
热议问题