Regex
regex
January 14, 20234 min read

Regex for Weekday

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 Weekdays are typically named Monday, Tuesday, Wednesday, Thursday and Friday. In this article let's understand how we can create a regex for weekday and how regex can be matched for a given weekday.

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 Weekday

The Weekday should have the following criteria and structure-

  • It should be a string
  • There are 5 days which are categorised as weekdays - Monday, Tuesday, Wednesday, Thursday, Friday
  • It can also be abbreviated as Mon, Tue, Wed, Thu, Fri

Regex for checking if Weekday is valid or not

Regular Expression for Weekday is-

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

Test string examples for the above regex-

Input StringMatch Output
Sundaydoes not match
Mondaymatches
holidaydoes not match
Wedmatches
thursdaymatches
Tue.matches
Tuesmatches

Here is a detailed explanation of the above regex-

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

1st Alternative ^(Mon|(T(ues|hurs))|Fri)(day|\.)?$
^ asserts position at start of a line
1st Capturing Group (Mon|(T(ues|hurs))|Fri)
1st Alternative Mon
Mon matches the characters Mon literally (case insensitive)
2nd 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)
3rd 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 T((ue?)|(hu?r?))\.?$
T matches the character T with index 8410 (5416 or 1248) literally (case insensitive)
6th Capturing Group ((ue?)|(hu?r?))
1st Alternative (ue?)
7th Capturing Group (ue?)
u matches the character u with index 11710 (7516 or 1658) literally (case insensitive)
e matches the character e with index 10110 (6516 or 1458) literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Alternative (hu?r?)
8th Capturing Group (hu?r?)
h matches the character h with index 10410 (6816 or 1508) literally (case insensitive)
u matches the character u with index 11710 (7516 or 1658) literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
r matches the character r with index 11410 (7216 or 1628) literally (case insensitive)
\. 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)
$ asserts position at the end of a line
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to check if the string is a valid Weekday 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, 20232 min read
Next Article
December 25, 20224 min read