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 String | Match Output |
---|---|
.as10 | does not match |
www.google.com | matches |
#@$some .qwq.eras | does not match |
something.debugpointer.com | matches |
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. Understanding the concept of domains and subdomains is crucial for effectively organizing and structuring websites. Subdomains provide a way to segregate content and functions within a domain. Moreover, regular expressions (regex) serve as powerful tools for text manipulation and validation. The provided regex pattern offers a comprehensive method to verify the validity of subdomains, aiding in various programming tasks.