regex: validate european date format with multiple separators

前端 未结 3 1858
孤街浪徒
孤街浪徒 2021-01-13 00:14

I want to validate european date formats like \"10.02.2012\" or \"10-02-2012\". Therefore I created the following regex:

/\\d[0-9]{2}(.|-)\\d[0-9]{2}(.|-)\\d         


        
3条回答
  •  天涯浪人
    2021-01-13 00:32

    Note that the answers provided so far using the \d character class will allow dates such as 00.00.0000 or 99.99.9999 to pass through. If you want to catch this, you need to use a more specific regex. The one below would allow valid dates between 01.01.1900 and 31.12.2099

    ^([1-9]|0[1-9]|[12][0-9]|3[01])[-\.]([1-9]|0[1-9]|1[012])[-\.](19|20)\d\d$
    

    If you want to allow any year from 0000 to 9999, then you could change it to

    ^([1-9]|0[1-9]|[12][0-9]|3[01])[-\.]([1-9]|0[1-9]|1[012])[-\.]\d{4}$
    

提交回复
热议问题