Skip to content
the poor coder

regx get only domain name from url

Poor Man
Published
Reading time
1 min read
On this page

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:

Text

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:

Text

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:

Text

$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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC