Regular Expression to match a valid day in a date

后端 未结 10 1771
别那么骄傲
别那么骄傲 2020-12-11 01:45

I need help coming up with a regex to make sure the user enters a valid date The string will be in the format of mm/dd/yyyy

Here is what I have come up

10条回答
  •  不知归路
    2020-12-11 02:14

    Simpler regex:

    ([12]\d|3[01]|0?[1-9])
    

    Consider the accepted answer and this expression:

    (0[1-9]|[12]\d|3[01])
    

    This matches 01 but not 1

    The other expression in the accepted answer:

    ([1-9]|[12]\d|3[01])
    

    This matches 1 but not 01

    It is not possible to add an OR clause to get them both working.

    The one I suggested matches both. Hope this helps.

提交回复
热议问题