regular expression for time

后端 未结 3 1165
别跟我提以往
别跟我提以往 2021-01-18 05:33

I want regular expression for HH:MM:SS AM/PM here HH must be 1-12 only, MM must 60 min, SS as usual (60 sec.) I any have it properly ...?

3条回答
  •  终归单人心
    2021-01-18 06:03

    If you want to be able to have the zero padding as optional as well as leap seconds:

    /^((0?[1-9])|(1[0-2])):[0-5]\d:(([0-5]\d)|(60)) [AP]M$/
    

    The breakdown:
    (0?[1-9]|1[0-2]) 1-9 (with optional leading zero) or 10-12
    [0-5][0-9] 00-59 (0-5 for first digit, 0-9 for second)
    ([0-5][0-9])|(60) Leap seconds
    [AP]M AM/PM

提交回复
热议问题