Trying to remake the date regex with http://html5pattern.com/Dates

(?: 19 | 20) [0-9] {2} - (? :( ?: 0 [1-9] | 1 [0-2]) - (?: 0 [1-9] | 1 [0 -9] | 2 [0-9]) | (? :( 02 ?! 02) (?: 0 [1-9] | 1 [0-2]) - (?: 30)) | (? :(? : 0 [13578] | 1 [02]) - 31))

under the format 01/31/2017

(?:(?:(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31)).(?:0[1-9]|1[0-2])).(?:19|20)[0-9]{2} 

30 and 31 days it does not accept, how to fix?

  • Rewrite .. Probably ... - Qwertiy
  • @Qwertiy is trying. The problem is that the number is checked for a month and exchanging places has deteriorated ... - CodeGust
  • Well, so much to turn into a preview, I guess. - Qwertiy

1 answer 1

Something like that?

 var regex = /(?:(?:0[1-9]|1[0-9]|2[0-9])-(?:0[1-9]|1[0-2])|(?:(?:30)-(?!02)(?:0[1-9]|1[0-2]))|(?:31-(?:0[13578]|1[02])))-(?:19|20)[0-9]{2}/; var dates = ["31-13-2017", "31-12-2017", "32-12-2017", "01-11-2017"]; for(var index in dates) { console.log("test " + dates[index] + ": " + regex.test(dates[index])); } 

  • Thank! Works (only on the points replaced). - CodeGust