December 23, 20222 min read

Regex for month

A date is a specific day or time period, often given as a combination of a month, date, and year. In this article let's understand how we can create a regex for month and how regex can be matched for a given month name.

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 month name

The month should have the following criteria and structure-

  • Month can also be a string like January, February, March, April, May, June, July, August, September, October, November, December
  • Month can also be a string like Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

Regex for checking if month name is valid or not

Regular Expression for month name-


Test string examples for the above regex-

Input StringMatch Output
Janudoes not match
Fedoes not match

Here is a detailed explanation of the above regex-


Non-capturing group (?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)
1st Alternative Jan(?:uary)?
2nd Alternative Feb(?:ruary)?
3rd Alternative Mar(?:ch)?
4th Alternative Apr(?:il)?
5th Alternative May
6th Alternative Jun(?:e)?
7th Alternative Jul(?:y)?
8th Alternative Aug(?:ust)?
9th Alternative Sep(?:tember)?
10th Alternative Oct(?:ober)?
11th Alternative (Nov|Dec)(?:ember)?
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 month name or not.

Share this blog
Tagged in :
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 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
December 24, 20224 min read
Next Article
December 23, 20222 min read