- Home
- Tools
- Developer Tools
- Regex Tester
- Date Regex
Date Regex
A regular expression for dates written as YYYY-MM-DD, with test lines that show what it catches and what it cannot know.
JavaScript syntax, run by your browser's own RegExp engine.
6 matches.
Matches
- 1
2024-02-29index 0 - 2
1999-12-31index 11 - 3
2026-11-22index 22 - 4
2023-02-29index 115 - 5
2024-02-30index 126 - 6
2024-04-31index 137
Cheat sheet
Characters
- .
- Any character except a line break
- \d \D
- A digit 0-9, anything but a digit
- \w \W
- A letter a-z, a digit or an underscore; anything else
- \s \S
- White space, anything but white space
- [abc] [^abc]
- One of a, b, c; any character except those
- [a-z0-9]
- A character in one of the ranges
- \p{L}
- A letter in any script (needs the u flag)
- \. \\ \/
- A literal dot, backslash, slash
How many
- a*
- Zero or more
- a+
- One or more
- a?
- Zero or one
- a{3}
- Exactly three
- a{2,5}
- Two to five
- a{2,}
- Two or more
- a*? a+? a??
- The same, but as few as possible (lazy)
Positions
- ^
- Start of the text, or of a line with the m flag
- $
- End of the text, or of a line with the m flag
- \b
- The edge of a word
- \B
- Anywhere that is not the edge of a word
Groups
- (abc)
- A group whose text is remembered
- (?<name>abc)
- The same, with a name
- (?:abc)
- A group that is not remembered
- a|b
- Either a or b
- \1 \k<name>
- The same text a group matched earlier
Looking around
- a(?=b)
- a, only when b follows
- a(?!b)
- a, only when b does not follow
- (?<=b)a
- a, only when b comes before it
- (?<!b)a
- a, only when b does not come before it
In the replacement
- $1 $2
- The text of group 1, group 2
- $<name>
- The text of a named group
- $&
- The whole match
- $` $'
- The text before the match, after the match
- $$
- A dollar sign
The pattern, piece by piece
For dates written as year, month and day with hyphens between them: the ISO 8601 form.
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/gm| Piece | What it does |
|---|---|
^ | The start of the line. |
\d{4} | Four digits for the year. |
- | A hyphen. |
(?:0[1-9]|1[0-2]) | The month: 01 to 09, or 10, 11 or 12. |
- | Another hyphen. |
(?:0[1-9]|[12]\d|3[01]) | The day: 01 to 09, 10 to 29, or 30 or 31. |
$ | The end of the line. |
What it gets right
These are the first lines in the box above. With the g and m flags every line is tested on its own, from its first character to its last.
| Line | Result | Why |
|---|---|---|
2024-02-29 | Match | |
1999-12-31 | Match | |
2026-11-22 | Match | |
2024-13-01 | No match | There is no month 13. |
2024-00-10 | No match | Nor a month 00. |
2024-01-32 | No match | No month has 32 days. |
24-01-05 | No match | A year of two digits. |
2024-1-5 | No match | Month and day without their zero. |
2024/01/05 | No match | Slashes. |
2024-01-05T10:30:00 | No match | A time behind the date. |
Where it goes wrong
The last lines in the box are there to show the limits: text the pattern refuses although it is fine, or lets through although it is not.
| Line | Result | What is wrong with that |
|---|---|---|
2023-02-29 | Match | 2023 was not a leap year. |
2024-02-30 | Match | February never has 30 days. |
2024-04-31 | Match | April has 30 days. |
Before you rely on it
The pattern checks the shape and the ranges, and that is where it stops. Which months have 30 days and which years are leap years is arithmetic, and a pattern that does arithmetic is a page long. Check the shape with the pattern and the day with code. In JavaScript, build the date and see whether it comes back the same: new Date(2023, 1, 29) turns out to be 1 March 2023, because months count from 0 and the 29th day spilled over.
Dates written year first sort correctly as plain text, which is why file names and databases use this form.
The second pattern accepts 05/06/2024, and nothing in those characters says whether it means 6 May or 5 June. Where the choice is yours, ask for the year first.
A pattern for dates written month first
The order used in the United States, with slashes, and with or without a zero in front of the month and the day. Like the first pattern, it lets February 30 through.
/^(?:0?[1-9]|1[0-2])\/(?:0?[1-9]|[12]\d|3[01])\/\d{4}$/gmRun on this text:
02/29/2024 2/9/2024 12/31/1999 13/01/2024 31/12/2024 02/29/24 2024-02-29
it finds 02/29/2024, 2/9/2024 and 12/31/1999, and nothing else.
The regex tester page explains the flags, has a cheat sheet of the syntax, and tells what happens when a pattern takes too long.