Regular expression for matching HH:MM time format

前端 未结 19 2736
甜味超标
甜味超标 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 17:44

    You can try the following

    ^\d{1,2}([:.]?\d{1,2})?([ ]?[a|p]m)?$
    

    It can detect the following patterns :

    2300 
    23:00 
    4 am 
    4am 
    4pm 
    4 pm
    04:30pm 
    04:30 pm 
    4:30pm 
    4:30 pm
    04.30pm
    04.30 pm
    4.30pm
    4.30 pm
    23:59 
    0000 
    00:00
    

提交回复
热议问题