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 Top Level Domain (TLD) into a web browser’s address bar or by clicking on a link. A Top Level Domain (TLD) 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 Top Level Domain (TLD)s, such as “support.example.com”, “blog.example.com”, and “store.example.com”. These Top Level Domain (TLD)s 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 Top Level Domain (TLD) and how regex can be matched for Top Level Domain (TLD).
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 Top Level Domain (TLD)
The Top Level Domain should have the following criteria and structure-
- It may or maynot contain
www.
or a optionally a Top Level Domain (TLD) - then it must be followed by Top Level Domain (TLD)
- then it will be followed by top level domain(TLD) like .com, .net, .io etc.,
Regex for checking if Top Level Domain (TLD) 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 Top Level Domain (TLD) or not. In conclusion, understanding domains and their corresponding Top Level Domains (TLDs) is essential for navigating the internet effectively. This article has provided insights into the structure of TLDs and how regular expressions (regex) can be employed to validate and match TLDs. By using regex patterns, developers and users alike can ensure the validity of TLDs and enhance their web experiences.