RegEx - Time Validation ((h)h:mm)

后端 未结 6 1658
春和景丽
春和景丽 2021-02-13 12:46
/^\\d{1,2}[:][0-5][0-9]$/

is what I have. this limits minutes to 00-59. It does not, however, limit hours to between 0 and 12. For similarity and unifo

6条回答
  •  一整个雨季
    2021-02-13 13:14

    The 0 - 9 and 10 - 12 cases need to be treated separately. (The two rules can be combined with |.)

    /^(?:0?\d|1[012]):[0-5]\d$/
    

    Here

    • (?:…) is a non-capturing group
    • x|y means match either pattern
    • 0?\d matches 0 - 9 or 00 - 09
    • 1[012] matches 10 - 12.

提交回复
热议问题