regx get only domain name from url

How to extract only the domain name from a URL using Regular Expressions

If you have a list of URLs and you want to extract only the domain name from each URL, you can do it using Regular Expressions (RegEx).

Method 1: Using the replace() method in JavaScript

You can use the replace() method in JavaScript to extract only the domain name from a URL. Here is an example:


var url = "https://www.example.com/path/to/page.html";
var domain = url.replace(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/i, "$1");
console.log(domain); // Output: example.com
  • The regular expression matches the protocol (http or https) and the www subdomain (if it exists) and captures the domain name.
  • The replace() method replaces the entire URL with the captured domain name.

Method 2: Using the URL object in JavaScript

You can also use the URL object in JavaScript to extract the hostname (which is equivalent to the domain name) from a URL. Here is an example:


var url = new URL("https://www.example.com/path/to/page.html");
var domain = url.hostname;
console.log(domain); // Output: example.com
  • The URL object parses the URL and provides various properties and methods to access its components.
  • The hostname property contains only the domain name.

Method 3: Using the parse_url() function in PHP

If you are working with PHP, you can use the parse_url() function to parse a URL into its components and then extract the domain name. Here is an example:


$url = "https://www.example.com/path/to/page.html";
$components = parse_url($url);
$domain = $components['host'];
echo $domain; // Output: example.com
  • The parse_url() function parses the URL into an associative array of components.
  • The 'host' key in the array contains only the domain name.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe