Regex
regex
December 25, 20224 min read

Regex for day of week

A day of the week is a period of time within a seven-day cycle, used to divide a calendar week into seven equal parts. The days of the week are typically named Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday. In this article let's understand how we can create a regex for day of a week and how regex can be matched for a given day of a week.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of a days of week

The day of week should have the following criteria and structure-

  • It should be a string
  • There are 7 days in a week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
  • It can also be abbreviated as Mon, Tue, Wed, Thu, Fri, Sat, Sun
  • It is also abbreviated as Mon, Tues, Wed, Thurs, Fri, Sat, Sun

Regex for checking if Day of week is valid or not

Regular Expression for Day of week is-

/^(Sun|Mon|(T(ues|hurs))|Fri)(day|\.)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|T((ue?)|(hu?r?))\.?$/igm

Test string examples for the above regex-

Input StringMatch Output
sunddoes not match
Sundaymatches
holidaydoes not match
Satmatches
thursdaymatches
Tue.matches
Tuesmatches

Here is a detailed explanation of the above regex-

/^(Sun|Mon|(T(ues|hurs))|Fri)(day|\.)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|T((ue?)|(hu?r?))\.?$/igm

1st Alternative (Sun|Mon|(T(ues|hurs))|Fri)(day|\.)?$
1st Capturing Group (Sun|Mon|(T(ues|hurs))|Fri)
1st Alternative Sun
Sun matches the characters Sun literally (case insensitive)
2nd Alternative Mon
Mon matches the characters Mon literally (case insensitive)
3rd Alternative (T(ues|hurs))
2nd Capturing Group (T(ues|hurs))
T matches the character T with index 8410 (5416 or 1248) literally (case insensitive)
3rd Capturing Group (ues|hurs)
1st Alternative ues
ues matches the characters ues literally (case insensitive)
2nd Alternative hurs
hurs matches the characters hurs literally (case insensitive)
4th Alternative Fri
Fri matches the characters Fri literally (case insensitive)
4th Capturing Group (day|\.)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative day
day matches the characters day literally (case insensitive)
2nd Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
$ asserts position at the end of a line
2nd Alternative Wed(\.|nesday)?$
Wed matches the characters Wed literally (case insensitive)
5th Capturing Group (\.|nesday)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
2nd Alternative nesday
nesday matches the characters nesday literally (case insensitive)
$ asserts position at the end of a line
3rd Alternative Sat(\.|urday)?$
Sat matches the characters Sat literally (case insensitive)
6th Capturing Group (\.|urday)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
2nd Alternative urday
urday matches the characters urday literally (case insensitive)
$ asserts position at the end of a line
4th Alternative T((ue?)|(hu?r?))\.?
T matches the character T with index 8410 (5416 or 1248) literally (case insensitive)
7th Capturing Group ((ue?)|(hu?r?))
1st Alternative (ue?)
8th Capturing Group (ue?)
2nd Alternative (hu?r?)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Global pattern flags
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

Hope this article was useful to check if the string is a valid days of the week or not.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
January 14, 20234 min read
Next Article
January 13, 20232 min read