Regular expression for matching HH:MM time format

前端 未结 19 2466
甜味超标
甜味超标 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:51

    You can use this regular expression:

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

    If you want to exclude 00:00, you can use this expression

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

    Second expression is better option because valid time is 00:01 to 00:59 or 0:01 to 23:59. You can use any of these upon your requirement. Regex101 link

提交回复
热议问题