regex: validate european date format with multiple separators

前端 未结 3 1884
孤街浪徒
孤街浪徒 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:30

    ok I see your problem...

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

    is redundant, in that \d is the same as [0-9], so actually here you are matching for 3 digits not 2. Try

    /\d{2}(\.|-)\d{2}(\.|-)\d{4}/
    

    or

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

    but don't use both

提交回复
热议问题