Published on

# Regex for Top Level Domain (TLD) 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 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 StringMatch Output
.as10does not match
#@$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)