I’m doing an exercise that asks me to write a regex. Here are the details:

The regex will match a string only if it contains at least one valid date, like:
[dd-mm]
Assuming the year in question is not a leap year.
The number of days each month should have are:

January 31 days
February 28 days
So on

All text outside a valid date can be ignored, including other invalid dates.

For example:

“[01-23]” // January 23rd is a valid date
“[02-31]” // February 31st is an invalid date
“[02-16]” // valid
“[ 6-03]” // invalid format
“ignored [08-11] ignored” // valid
“[3] [12-04] [09-tenth]” // December 4th is a valid date

How should I approach this? I guess there’s no need to write one regex for each month…

I’m learning regex today. And this exercise is from a website.

You’ll want to group all of the months that have the same number of days, and then setup a regex that checks for a range of numbers that proceed those months.

For Feb it would look something like this:

/02-(0[1-9]|1[0-9]|2[0-8])/

If March also had 28 days then you could match it as well like this:

/(02|03)-(0[1-9]|1[0-9]|2[0-8])/

As an aside, here’s a really great interactive tutorial to learning regex http://tryregex.com/

Sorry, I don’t understand the logic of the day part. The first digit for the day can only go from 0 to 2, the second digit can go from 0 to 8 if the first is a two, but from 0 to 9 if the first digit is a 0 or a 1.

I can say “or” with “|” in regex but can I say “and”?

I was thinking of this:

If the first digit is 0 or 1, the second day digit can go from 0 to 9, if the first digit is a 2, then the second digit can go from 0 to 8. *

Thanks a lot. What happens if I’d like to match a date with square bracket literals, like:> [mm-dd]. How do I escape the square brackets?

I tried this:

var validDate = /(\[){1}(01|03|05|07|08|10|12)-(0[1-9]|1[0-9]|2[0-9]|3[01])|(04|06|09|11)-(0[1-9]|1[0-9]|2[0-9]|30)|02-(0[1-9]|1[0-9]|2[0-8])(\]{1})/;

And the test almost passes except for:

Input: [01-11d should be invalid but was found valid.
Input: [01-10s should be invalid but was found valid.
Input: [12-29e should be invalid but was found valid.
Input: [12-27d should be invalid but was found valid.

Sorry, I asked so many questions. I read all the chapters in my JS books on regex and there’s nothing about this complex patterns. So thanks again.