Extracting Domain Name from URLs: A Tutorial

function GET_DOMAIN(url){

 let domain = ”;

 let matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);

 if(matches && matches[1]){

   domain = matches[1];

 }

 return domain;

}

The given code defines a function called GET_DOMAIN that extracts and returns the domain name from a given URL. The domain name represents the network location of a website. The function follows these steps:

  1. It starts by declaring and initializing two variables: domain and matches. The domain variable will store the extracted domain name, while the matches variable will be used to store the result of the regular expression match.
  2. The regular expression pattern used in the match method is /^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i. Let’s break down this pattern:
    • ^ asserts the start of the string.
    • https? matches either “http” or “https”.
    • \:\/\/ matches the literal characters “://”.
    • ([^\/?#]+) captures one or more characters that are not “/”, “?”, or “#” as a group.
    • (?:[\/?#]|$) matches either “/”, “?”, or “#” non-capturing group or the end of the string.
    • /i specifies that the match should be case-insensitive.
  3. This regular expression pattern is used to match and capture the domain name portion of the URL.
  4. The match method is called on the url string, passing the regular expression pattern as an argument. The method attempts to find a match for the pattern in the url string.
  5. The result of the match method is stored in the matches variable. If a match is found, the matches variable will be an array containing the entire matched string as the first element, followed by the captured groups.
  6. The if statement checks if matches is truthy (not null or undefined) and if matches[1] exists. Since we are interested in the captured group at index 1 (the domain name), the condition checks if the element at index 1 of matches exists.
  7. If the condition in the if statement is true, it means a match was found, and the domain name is extracted from matches[1]. The domain name is assigned to the domain variable.
  8. Finally, the domain variable, which either contains the extracted domain name or an empty string, is returned as the result of the function.

To understand how this function works, let’s consider an example:

GET_DOMAIN(‘https://www.example.com/path/to/page.html’)

The function will extract the domain name from the given URL and return it.

In this case, the regular expression pattern matches the “https://” portion of the URL and captures “www.example.com” as the domain name.

The extracted domain name is then assigned to the domain variable, and the function returns “www.example.com” as the result.

In summary, the GET_DOMAIN function extracts and returns the domain name from a given URL by using a regular expression match to capture the relevant portion of the URL.

Input (url)Output
https://www.example.comwww.example.com
http://example.com/index.htmlexample.com
https://subdomain.example.org/path?query=stringsubdomain.example.org
http://localhost:3000/localhost:3000