I’m working on a regex that’s supposed to match month-day-year gregorian dates (in javascript), and it works fine for January, March, May, July, August, October, and December (31-day months) dates, but for some reason which I haven’t been able to figure out, it will not match any date from any other month.
A little explanation of the code below: I’ve set it up to first match the month-day part of the date all at once, so it doesn’t match on February 31st, for example. So, I currently have it matching (((a 31-day month) THEN (a non-alphanumeric character, captured) THEN (a day from 1 through 31)) OR ((a 28-day month) THEN (a non-alphanumeric character, captured) THEN (a day from 1 through 29)) OR ((a 30-day month) THEN (a non-alphanumeric character, captured) THEN (a day from 1 through 30))) THEN (the previously captured character) THEN (a past or current year AD).
I’m sure the problem is simple, but I’m just not seeing it.
^
(?:
(?:
(?:0?[13578]|1[02])
([^\dA-Za-z])
(?:0?[1-9]|1[0-9]|2[0-9]|3[01])
)
|
(?:
(?:0?2)
([^\dA-Za-z])
(?:0?[1-9]|1[0-9]|2[0-9])
)
|
(?:
(?:0?[469]|11)
([^\dA-Za-z])
(?:0?[1-9]|1[0-9]|2[0-9]|30)
)
)
\1
(?:0{0,3}[1-9]|0{0,2}[1-9][0-9]|0?[1-9][0-9]{2}|1[0-9]{3}|200[0-9]|201[01])
$
You get this error because you’re using
\1, which doesn’t match for the last 2 alternations.\1refers to the first([^\dA-Za-z])– if it didn’t match, it cannot get to the year.A simple alternative is
(?:\1|\2|\3).Another option is to add at the start of the pattern
(?=\d+([^\dA-Za-z])), and use\1on all places. For example:On a side note: yikes! try Datejs – an impressive open-source JavaScript date library.