Regex
regex
December 19, 20224 min read

Regex for Subdomain validation

A domain is a unique name that identifies a website or other resource on the internet. It is used to access a website or resource by entering the Subdomain into a web browser's address bar or by clicking on a link. A subdomain is a domain that is part of a larger domain. It is a way to organize and structure a website into different sections or categories, each with its own unique web address.

For example, consider the domain "example.com". This domain could have a number of subdomains, such as "support.example.com", "blog.example.com", and "store.example.com". These subdomains can be used to host different sections or pages of a website, or to host completely separate websites with their own content and functionality.

In this article let's understand how we can create a regex for Subdomain and how regex can be matched for Subdomain.

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 Subdomain

The subdomain should have the following criteria and structure-

  • It may or maynot contain www. or a optionally a subdomain
  • then it must be followed by Subdomain
  • then it will be followed by top level domain(TLD) like .com, .net, .io etc.,

Regex for checking if Subdomain is valid or not

Regular Expression-

/^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?/igm

Test string examples for the above regex-

Input StringMatch Output
.as10does not match
www.google.commatches
#@$some .qwq.erasdoes not match
something.debugpointer.commatches

Here is a detailed explanation of the above regex-

/^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?/igm

Match a single character present in the list below [A-Za-z0-9]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
Non-capturing group (?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [A-Za-z0-9\-]
{0,61} matches the previous token between 0 and 61 times, as many times as possible, giving back as needed (greedy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
\- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
Match a single character present in the list below [A-Za-z0-9]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
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 Subdomain 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
December 17, 20225 min read
Next Article