Extract hostname name from string
Asked Answered
O

29

333

I would like to match just the root of a URL and not the whole URL from a text string. Given:

http://www.youtube.com/watch?v=ClkQA2Lb_iE
http://youtu.be/ClkQA2Lb_iE
http://www.example.com/12xy45
http://example.com/random

I want to get the 2 last instances resolving to the www.example.com or example.com domain.

I heard regex is slow and this would be my second regex expression on the page so If there is anyway to do it without regex let me know.

I'm seeking a JS/jQuery version of this solution.

Outrageous answered 14/12, 2011 at 1:36 Comment(2)
Would recommend to change accepted answer for new people coming into this question, since Robin's answer is much better.Supertonic
(also maybe remove the "heard regex is slow" from your question so you don't give away misinformed ideas to newbies, since regex is the fastest solution in the benchmark)Supertonic
H
304

There is no need to parse the string, just pass your URL as an argument to URL constructor:

const url = 'http://www.youtube.com/watch?v=ClkQA2Lb_iE';
const { hostname } = new URL(url);

console.assert(hostname === 'www.youtube.com');
Hearst answered 5/2, 2016 at 11:22 Comment(5)
2021 and beyond, should this be accepted answer?Sprocket
Yes, why shouldn't it be? Works perfectly, and is nice and clean using object destructing developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Hofuf
The curly braces are needed because an object is returned and hostname is one property of it. It is destructuring as @Hofuf wrote in his comment.Longe
If you need the hostname with https://, you do const { hostname,protocol } = new URL(url);Longe
@Longe I believe you can just use { origin } for that, it handles that for youMidinette
E
340

A neat trick without using regular expressions:

var tmp        = document.createElement ('a');
;   tmp.href   = "http://www.example.com/12xy45";

// tmp.hostname will now contain 'www.example.com'
// tmp.host will now contain hostname and port 'www.example.com:80'

Wrap the above in a function such as the below and you have yourself a superb way of snatching the domain part out of an URI.

function url_domain(data) {
  var    a      = document.createElement('a');
         a.href = data;
  return a.hostname;
}
Earlineearls answered 14/12, 2011 at 1:48 Comment(1)
Don't use this if you need to do it fast. It's about 40-60 times slower than gilly3's method. Tested in jsperf: jsperf.com/hostname-from-url.Geiger
D
325

I give you 3 possible solutions:

  1. Using an npm package psl that extract anything you throw at it.
  2. Using my custom implementation extractRootDomain which works with most cases.
  3. URL(url).hostname works, but not for every edge case. Click "Run Snippet" to see how it run against them.

1. Using npm package psl (Public Suffix List)

The "Public Suffix List" is a list of all valid domain suffixes and rules, not just Country Code Top-Level domains, but unicode characters as well that would be considered the root domain (i.e. www.食狮.公司.cn, b.c.kobe.jp, etc.). Read more about it here.

Try:

npm install --save psl

Then with my "extractHostname" implementation run:

let psl = require('psl');
let url = 'http://www.youtube.com/watch?v=ClkQA2Lb_iE';
psl.get(extractHostname(url)); // returns youtube.com

2. My Custom Implementation of extractRootDomain

Below is my implementation and it also runs against a variety of possible URL inputs.

function extractHostname(url) {
  var hostname;
  //find & remove protocol (http, ftp, etc.) and get hostname

  if (url.indexOf("//") > -1) {
    hostname = url.split('/')[2];
  } else {
    hostname = url.split('/')[0];
  }

  //find & remove port number
  hostname = hostname.split(':')[0];
  //find & remove "?"
  hostname = hostname.split('?')[0];

  validateDomain(hostname);
  return hostname;
}

// Warning: you can use this function to extract the "root" domain, but it will not be as accurate as using the psl package.

function extractRootDomain(url) {
  var domain = extractHostname(url),
  splitArr = domain.split('.'),
  arrLen = splitArr.length;

  //extracting the root domain here
  //if there is a subdomain
  if (arrLen > 2) {
    domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
    //check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
    if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
      //this is using a ccTLD
      domain = splitArr[arrLen - 3] + '.' + domain;
    }
  }
  validateDomain(domain);
  return domain;
}

const urlHostname = url => {
  try {
    return new URL(url).hostname;
  }
  catch(e) { return e; }
};

const validateDomain = s => {
  try {
    new URL("https://" + s);
    return true;
  }
  catch(e) {
    console.error(e);
    return false;
  }
};

const urls = [
    "http://www.blog.classroom.me.uk/index.php",
    "http://www.youtube.com/watch?v=ClkQA2Lb_iE",
    "https://www.youtube.com/watch?v=ClkQA2Lb_iE",
    "www.youtube.com/watch?v=ClkQA2Lb_iE",
    "ftps://ftp.websitename.com/dir/file.txt",
    "websitename.com:1234/dir/file.txt",
    "ftps://websitename.com:1234/dir/file.txt",
    "example.com?param=value",
    "https://facebook.github.io/jest/",
    "//youtube.com/watch?v=ClkQA2Lb_iE",
    "www.食狮.公司.cn",
    "b.c.kobe.jp",
    "a.d.kyoto.or.jp",
    "http://localhost:4200/watch?v=ClkQA2Lb_iE",
    "a.d.kyoto.or.j|p",
];

const test = (method, arr) => console.log(
`=== Testing "${method.name}" ===\n${arr.map(url => method(url)).join("\n")}\n`);

test(extractHostname, urls);
test(extractRootDomain, urls);
test(urlHostname, urls);

Regardless having the protocol or even port number, you can extract the domain. This is a very simplified, non-regex solution, so I think this will do given the data set we were provided in the question.

This does not provide any sort of domain name validation, so if you'd like to add one in, you can do so yourself by

3. URL(url).hostname

URL(url).hostname is a valid solution but it doesn't work well with some edge cases that I have addressed. As you can see in my last test, it doesn't like some of the URLs. You can definitely use a combination of my solutions to make it all work though.

*Thank you @Timmerz, @renoirb, @rineez, @BigDong, @ra00l, @ILikeBeansTacos, @CharlesRobertson for your suggestions! @ross-allen, thank you for reporting the bug!

Desmund answered 30/5, 2014 at 0:6 Comment(9)
121KB gzipped bundlephobia.com/package/[email protected]. That's more than 17 times heavier than React. Unless you have a really good reason not to use URL().hostname, this is a really bad solution and probably the slowest of all (because of its bundle size)Scrubby
@RobinMétral, it's probably slow and large because it has to handle all the requirements for this big list publicsuffix.org/list/public_suffix_list.dat I'm not sure if URL().hostname would suffice.Desmund
Totally agree here, which is why I would default to URL().hostname and only resort to psl if there is an explicit need for something more :)Scrubby
@robin-métral See the last set of tests I run. URL().hostname may not be suitable given some edge cases. Also, my implementions of extractRootDomain and extractHostname don't require psl.Desmund
@LewisNakao: the standard and low overhead ( in * ways) solution "may not " be suitable? well, is it or isn't it because pulling in some huge dependency or some rando's hand-rolled parser may be an awful idea. it is in fact an awful idea, I just liked the word play.Troudeloup
@LewisNakao and what URL 'doesn't handle' is invalid input. your parser isn't compliant. "a.d.kyoto.or.j|p" should fail extractHostname due to the invalid '|'. it's the first test I tried. your parser is broken and URL may not be. URL is not, but word play. url.spec.whatwg.org/#concept-host-parserTroudeloup
You're right, my rando parser isn't great, but you can always tweak it to work with your edge case data. OP wanted a non-regex solution so it's not so easy to fail inputs with odd characters in them. I'm not going to click on your suspicious link, btw.Desmund
The "suspicious link" is the HTML living standard :PScrubby
I added a simple URL validation method to address invalid inputs: const validateDomain = s => { try { new URL("https://" + s); return true; } catch(e) { console.error(e); return false; } }; Desmund
H
304

There is no need to parse the string, just pass your URL as an argument to URL constructor:

const url = 'http://www.youtube.com/watch?v=ClkQA2Lb_iE';
const { hostname } = new URL(url);

console.assert(hostname === 'www.youtube.com');
Hearst answered 5/2, 2016 at 11:22 Comment(5)
2021 and beyond, should this be accepted answer?Sprocket
Yes, why shouldn't it be? Works perfectly, and is nice and clean using object destructing developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Hofuf
The curly braces are needed because an object is returned and hostname is one property of it. It is destructuring as @Hofuf wrote in his comment.Longe
If you need the hostname with https://, you do const { hostname,protocol } = new URL(url);Longe
@Longe I believe you can just use { origin } for that, it handles that for youMidinette
P
155

Try this:

var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
var domain = matches && matches[1];  // domain will be null if no match is found

If you want to exclude the port from your result, use this expression instead:

/^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/i

Edit: To prevent specific domains from matching, use a negative lookahead. (?!youtube.com)

/^https?\:\/\/(?!(?:www\.)?(?:youtube\.com|youtu\.be))([^\/:?#]+)(?:[\/:?#]|$)/i
Pettway answered 14/12, 2011 at 1:42 Comment(0)
L
131

There are two good solutions for this, depending on whether you need to optimize for performance or not (and without external dependencies!):

1. Use URL.hostname for readability

The cleanest and easiest solution is to use URL.hostname.

const getHostname = (url) => {
  // use URL constructor and return hostname
  return new URL(url).hostname;
}

// tests
console.log(getHostname("https://mcmap.net/q/98317/-extract-hostname-name-from-string/"));
console.log(getHostname("https://developer.mozilla.org/en-US/docs/Web/API/URL/hostname"));

URL.hostname is part of the URL API, supported by all major browsers except IE (caniuse). Use a URL polyfill if you need to support legacy browsers.

Bonus: using the URL constructor will also give you access to other URL properties and methods!


2. Use RegEx for performance

URL.hostname should be your choice for most use cases. However, it's still much slower than this regex (test it yourself on jsPerf):

const getHostnameFromRegex = (url) => {
  // run against regex
  const matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
  // extract hostname (will be null if no match is found)
  return matches && matches[1];
}

// tests
console.log(getHostnameFromRegex("https://mcmap.net/q/98317/-extract-hostname-name-from-string/"));
console.log(getHostnameFromRegex("https://developer.mozilla.org/en-US/docs/Web/API/URL/hostname"));

TL;DR

You should probably use URL.hostname. If you need to process an incredibly large number of URLs (where performance would be a factor), consider RegEx.

Lass answered 1/3, 2019 at 15:33 Comment(0)
M
38

Parsing a URL can be tricky because you can have port numbers and special chars. As such, I recommend using something like parseUri to do this for you. I doubt performance is going to be a issue unless you are parsing hundreds of URLs.

Massacre answered 14/12, 2011 at 1:43 Comment(1)
Don't use this if you need to do it fast. For just getting the hostname, it's about 40-60 times slower than gilly3's method. Tested in jsperf: jsperf.com/hostname-from-url.Geiger
D
21

If you end up on this page and you are looking for the best REGEX of URLS try this one:

^(?:https?:)?(?:\/\/)?([^\/\?]+)

https://regex101.com/r/pX5dL9/1

You can use it like below and also with case insensitive manner to match with HTTPS and HTTP as well.:

const match = str.match(/^(?:https?:)?(?:\/\/)?([^\/\?]+)/i);
const hostname = match && match[1];

It works for urls without http:// , with http, with https, with just // and dont grab the path and query path as well.

Good Luck

Depoliti answered 11/11, 2015 at 13:17 Comment(3)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewApriorism
Edited and submited the regex :)Depoliti
TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ Teaching people to use regex to parse a url is like teaching them to remove a lock with an AR-15.Hildebrandt
H
20

I tried to use the Given solutions, the Chosen one was an overkill for my purpose and "Creating a element" one messes up for me.

It's not ready for Port in URL yet. I hope someone finds it useful

function parseURL(url){
    parsed_url = {}

    if ( url == null || url.length == 0 )
        return parsed_url;

    protocol_i = url.indexOf('://');
    parsed_url.protocol = url.substr(0,protocol_i);

    remaining_url = url.substr(protocol_i + 3, url.length);
    domain_i = remaining_url.indexOf('/');
    domain_i = domain_i == -1 ? remaining_url.length - 1 : domain_i;
    parsed_url.domain = remaining_url.substr(0, domain_i);
    parsed_url.path = domain_i == -1 || domain_i + 1 == remaining_url.length ? null : remaining_url.substr(domain_i + 1, remaining_url.length);

    domain_parts = parsed_url.domain.split('.');
    switch ( domain_parts.length ){
        case 2:
          parsed_url.subdomain = null;
          parsed_url.host = domain_parts[0];
          parsed_url.tld = domain_parts[1];
          break;
        case 3:
          parsed_url.subdomain = domain_parts[0];
          parsed_url.host = domain_parts[1];
          parsed_url.tld = domain_parts[2];
          break;
        case 4:
          parsed_url.subdomain = domain_parts[0];
          parsed_url.host = domain_parts[1];
          parsed_url.tld = domain_parts[2] + '.' + domain_parts[3];
          break;
    }

    parsed_url.parent_domain = parsed_url.host + '.' + parsed_url.tld;

    return parsed_url;
}

Running this:

parseURL('https://www.facebook.com/100003379429021_356001651189146');

Result:

Object {
    domain : "www.facebook.com",
    host : "facebook",
    path : "100003379429021_356001651189146",
    protocol : "https",
    subdomain : "www",
    tld : "com"
}
Howitzer answered 5/6, 2013 at 8:16 Comment(1)
no one should ever use this codeTroudeloup
M
8

All url properties, no dependencies, no JQuery, easy to understand

This solution gives your answer plus additional properties. No JQuery or other dependencies required, paste and go.

Usage

getUrlParts("https://news.google.com/news/headlines/technology.html?ned=us&hl=en")

Output

{
  "origin": "https://news.google.com",
  "domain": "news.google.com",
  "subdomain": "news",
  "domainroot": "google.com",
  "domainpath": "news.google.com/news/headlines",
  "tld": ".com",
  "path": "news/headlines/technology.html",
  "query": "ned=us&hl=en",
  "protocol": "https",
  "port": 443,
  "parts": [
    "news",
    "google",
    "com"
  ],
  "segments": [
    "news",
    "headlines",
    "technology.html"
  ],
  "params": [
    {
      "key": "ned",
      "val": "us"
    },
    {
      "key": "hl",
      "val": "en"
    }
  ]
}

Code
The code is designed to be easy to understand rather than super fast. It can be called easily 100 times per second, so it's great for front end or a few server usages, but not for high volume throughput.

function getUrlParts(fullyQualifiedUrl) {
    var url = {},
        tempProtocol
    var a = document.createElement('a')
    // if doesn't start with something like https:// it's not a url, but try to work around that
    if (fullyQualifiedUrl.indexOf('://') == -1) {
        tempProtocol = 'https://'
        a.href = tempProtocol + fullyQualifiedUrl
    } else
        a.href = fullyQualifiedUrl
    var parts = a.hostname.split('.')
    url.origin = tempProtocol ? "" : a.origin
    url.domain = a.hostname
    url.subdomain = parts[0]
    url.domainroot = ''
    url.domainpath = ''
    url.tld = '.' + parts[parts.length - 1]
    url.path = a.pathname.substring(1)
    url.query = a.search.substr(1)
    url.protocol = tempProtocol ? "" : a.protocol.substr(0, a.protocol.length - 1)
    url.port = tempProtocol ? "" : a.port ? a.port : a.protocol === 'http:' ? 80 : a.protocol === 'https:' ? 443 : a.port
    url.parts = parts
    url.segments = a.pathname === '/' ? [] : a.pathname.split('/').slice(1)
    url.params = url.query === '' ? [] : url.query.split('&')
    for (var j = 0; j < url.params.length; j++) {
        var param = url.params[j];
        var keyval = param.split('=')
        url.params[j] = {
            'key': keyval[0],
            'val': keyval[1]
        }
    }
    // domainroot
    if (parts.length > 2) {
        url.domainroot = parts[parts.length - 2] + '.' + parts[parts.length - 1];
        // check for country code top level domain
        if (parts[parts.length - 1].length == 2 && parts[parts.length - 1].length == 2)
            url.domainroot = parts[parts.length - 3] + '.' + url.domainroot;
    }
    // domainpath (domain+path without filenames) 
    if (url.segments.length > 0) {
        var lastSegment = url.segments[url.segments.length - 1]
        var endsWithFile = lastSegment.indexOf('.') != -1
        if (endsWithFile) {
            var fileSegment = url.path.indexOf(lastSegment)
            var pathNoFile = url.path.substr(0, fileSegment - 1)
            url.domainpath = url.domain
            if (pathNoFile)
                url.domainpath = url.domainpath + '/' + pathNoFile
        } else
            url.domainpath = url.domain + '/' + url.path
    } else
        url.domainpath = url.domain
    return url
}
Mixed answered 24/10, 2017 at 18:12 Comment(5)
fails at some pretty simple parsing. Try getUrlParts('www.google.com') in a console on this page.Outrageous
@Outrageous That's not a url, url's have a protocol. However I've updated the code to handle the more general case so please take back your downvote.Mixed
I didn't down vote you. But I would have if I wasn't specifically asking for http:// in my original question.Outrageous
@Lee fails at this input: var url="https://mail.gggg.google.cn/link/link/link"; the domainroot should be google.com but it outputs: gggg.google.cn while the gggg is a sub-domain (domains can have multiple sub-domains).Cristoforo
terrible. why do people do this?Troudeloup
D
8

Just use the URL() constructor:

new URL(url).host
Darladarlan answered 6/2, 2020 at 15:41 Comment(1)
same as answer given by @Hearst https://mcmap.net/q/98317/-extract-hostname-name-from-string and also #8499092Outrageous
T
5
function hostname(url) {
    var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
    if ( match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0 ) return match[2];
}

The above code will successfully parse the hostnames for the following example urls:

http://WWW.first.com/folder/page.html first.com

http://mail.google.com/folder/page.html mail.google.com

https://mail.google.com/folder/page.html mail.google.com

http://www2.somewhere.com/folder/page.html?q=1 somewhere.com

https://www.another.eu/folder/page.html?q=1 another.eu

Original credit goes to: http://www.primaryobjects.com/CMS/Article145

Twedy answered 6/8, 2015 at 5:47 Comment(0)
H
5

Was looking for a solution to this problem today. None of the above answers seemed to satisfy. I wanted a solution that could be a one liner, no conditional logic and nothing that had to be wrapped in a function.

Here's what I came up with, seems to work really well:

hostname="http://www.example.com:1234"
hostname.split("//").slice(-1)[0].split(":")[0].split('.').slice(-2).join('.')   // gives "example.com"

May look complicated at first glance, but it works pretty simply; the key is using 'slice(-n)' in a couple of places where the good part has to be pulled from the end of the split array (and [0] to get from the front of the split array).

Each of these tests return "example.com":

"http://example.com".split("//").slice(-1)[0].split(":")[0].split('.').slice(-2).join('.')
"http://example.com:1234".split("//").slice(-1)[0].split(":")[0].split('.').slice(-2).join('.')
"http://www.example.com:1234".split("//").slice(-1)[0].split(":")[0].split('.').slice(-2).join('.')
"http://foo.www.example.com:1234".split("//").slice(-1)[0].split(":")[0].split('.').slice(-2).join('.')
Haworth answered 21/4, 2017 at 21:30 Comment(1)
nice because it handles a case where www is irrelevantOutrageous
H
5

Here's the jQuery one-liner:

$('<a>').attr('href', url).prop('hostname');
Heda answered 10/6, 2017 at 6:51 Comment(0)
I
5

This is not a full answer, but the below code should help you:

function myFunction() {
    var str = "https://www.123rf.com/photo_10965738_lots-oop.html";
    matches = str.split('/');
    return matches[2];
}

I would like some one to create code faster than mine. It help to improve my-self also.

Imponderable answered 31/8, 2017 at 13:56 Comment(0)
S
4

Okay, I know this is an old question, but I made a super-efficient url parser so I thought I'd share it.

As you can see, the structure of the function is very odd, but it's for efficiency. No prototype functions are used, the string doesn't get iterated more than once, and no character is processed more than necessary.

function getDomain(url) {
    var dom = "", v, step = 0;
    for(var i=0,l=url.length; i<l; i++) {
        v = url[i]; if(step == 0) {
            //First, skip 0 to 5 characters ending in ':' (ex: 'https://')
            if(i > 5) { i=-1; step=1; } else if(v == ':') { i+=2; step=1; }
        } else if(step == 1) {
            //Skip 0 or 4 characters 'www.'
            //(Note: Doesn't work with www.com, but that domain isn't claimed anyway.)
            if(v == 'w' && url[i+1] == 'w' && url[i+2] == 'w' && url[i+3] == '.') i+=4;
            dom+=url[i]; step=2;
        } else if(step == 2) {
            //Stop at subpages, queries, and hashes.
            if(v == '/' || v == '?' || v == '#') break; dom += v;
        }
    }
    return dom;
}
Somersault answered 2/11, 2016 at 21:20 Comment(0)
A
4

oneline with jquery

$('<a>').attr('href', document.location.href).prop('hostname');
Amphistylar answered 16/5, 2018 at 9:43 Comment(0)
G
3
// use this if you know you have a subdomain
// www.domain.com -> domain.com
function getDomain() {
  return window.location.hostname.replace(/([a-zA-Z0-9]+.)/,"");
}
Gravettian answered 1/5, 2012 at 5:19 Comment(0)
H
3
String.prototype.trim = function(){return his.replace(/^\s+|\s+$/g,"");}
function getHost(url){
    if("undefined"==typeof(url)||null==url) return "";
    url = url.trim(); if(""==url) return "";
    var _host,_arr;
    if(-1<url.indexOf("://")){
        _arr = url.split('://');
        if(-1<_arr[0].indexOf("/")||-1<_arr[0].indexOf(".")||-1<_arr[0].indexOf("\?")||-1<_arr[0].indexOf("\&")){
            _arr[0] = _arr[0].trim();
            if(0==_arr[0].indexOf("//")) _host = _arr[0].split("//")[1].split("/")[0].trim().split("\?")[0].split("\&")[0];
            else return "";
        }
        else{
            _arr[1] = _arr[1].trim();
            _host = _arr[1].split("/")[0].trim().split("\?")[0].split("\&")[0];
        }
    }
    else{
        if(0==url.indexOf("//")) _host = url.split("//")[1].split("/")[0].trim().split("\?")[0].split("\&")[0];
        else return "";
    }
    return _host;
}
function getHostname(url){
    if("undefined"==typeof(url)||null==url) return "";
    url = url.trim(); if(""==url) return "";
    return getHost(url).split(':')[0];
}
function getDomain(url){
    if("undefined"==typeof(url)||null==url) return "";
    url = url.trim(); if(""==url) return "";
    return getHostname(url).replace(/([a-zA-Z0-9]+.)/,"");
}
Heavily answered 25/6, 2015 at 9:32 Comment(1)
so i add comments here: That code works even with url which starts from // or have syntax errors like qqq.qqq.qqq&test=2 or have query param with URL like ?param=www.wwwHeavily
L
3

I personally researched a lot for this solution, and the best one I could find is actually from CloudFlare's "browser check":

function getHostname(){  
            secretDiv = document.createElement('div');
            secretDiv.innerHTML = "<a href='/'>x</a>";
            secretDiv = secretDiv.firstChild.href;
            var HasHTTPS = secretDiv.match(/https?:\/\//)[0];
            secretDiv = secretDiv.substr(HasHTTPS.length);
            secretDiv = secretDiv.substr(0, secretDiv.length - 1);
            return(secretDiv);  
}  

getHostname();

I rewritten variables so it is more "human" readable, but it does the job better than expected.

Linwoodlinz answered 5/2, 2018 at 12:4 Comment(0)
P
3

Well, doing using an regular expression will be a lot easier:

    mainUrl = "http://www.mywebsite.com/mypath/to/folder";
    urlParts = /^(?:\w+\:\/\/)?([^\/]+)(.*)$/.exec(mainUrl);
    host = Fragment[1]; // www.mywebsite.com
Parade answered 24/9, 2018 at 10:54 Comment(0)
A
2

in short way you can do like this

var url = "http://www.someurl.com/support/feature"

function getDomain(url){
  domain=url.split("//")[1];
  return domain.split("/")[0];
}
eg:
  getDomain("http://www.example.com/page/1")

  output:
   "www.example.com"

Use above function to get domain name

Ablaut answered 17/5, 2016 at 13:39 Comment(3)
what is problem?Ablaut
the problem is it won't work if there is no slash before ?Daffie
in your case you need to check for ? in your domain name string and instead of return domain.split("/")[0]; put this return domain.split("?")[0]; hope it workAblaut
H
1
import URL from 'url';

const pathname = URL.parse(url).path;
console.log(url.replace(pathname, ''));

this takes care of both the protocol.

Hate answered 15/4, 2020 at 6:7 Comment(2)
Indeed this module is provided with NodeJS.Hammock
DO NOT USE. This is a legacy NodeJS API (check the docs). The newer WHATWG API is the same as in the browser: don't import "url" and just use a constructor: new URL(). It has been covered extensively in other answers. Finally, the question is about getting the hostname, but this answer just removes the path (it's not the same thing). Downvoted.Scrubby
R
1

This solution works well and you can also use if URL contains a lot of invalid characters.

install psl package

npm install --save psl

implementation

const psl = require('psl');

const url= new URL('http://www.youtube.com/watch?v=ClkQA2Lb_iE').hostname;
const parsed = psl.parse(url);

console.log(parsed)

output:

{
  input: 'www.youtube.com',
  tld: 'com',
  sld: 'youtube',
  domain: 'youtube.com',
  subdomain: 'www',
  listed: true
}
Rancho answered 17/11, 2022 at 12:10 Comment(1)
Same as stackoverflow.com/a/23945027, and psl is slow+heavyScrubby
W
0

Code:

var regex = /\w+.(com|co\.kr|be)/ig;
var urls = ['http://www.youtube.com/watch?v=ClkQA2Lb_iE',
            'http://youtu.be/ClkQA2Lb_iE',
            'http://www.example.com/12xy45',
            'http://example.com/random'];


$.each(urls, function(index, url) {
    var convertedUrl = url.match(regex);
    console.log(convertedUrl);
});

Result:

youtube.com
youtu.be
example.com
example.com
Wildfire answered 8/9, 2016 at 8:48 Comment(3)
@ChristianTernus On the contrary; the OP mentioned regex, and this is pretty obviously a regex expression designed to match the requested portion of a URL. It's not entirely correct (e.g. it requires www. even though not all URLs have this component), but it is certainly an answer.Intermarry
@KyleStrand Pretty obviously is a subjective judgement; providing a raw regex when asked "I'm seeking a JS/jQuery version of this solution" doesn't answer the qeustion.Didymous
I'm the OP. I was a new developer at the time seeking an out of the box solution in JS. Indeed, a raw regex string without any context would not have helped at all. Plus it's incomplete.Outrageous
M
0

parse-domain - a very solid lightweight library

npm install parse-domain

const { fromUrl, parseDomain } = require("parse-domain");

Example 1

parseDomain(fromUrl("http://www.example.com/12xy45"))
{ type: 'LISTED',
  hostname: 'www.example.com',
  labels: [ 'www', 'example', 'com' ],
  icann:
   { subDomains: [ 'www' ],
     domain: 'example',
     topLevelDomains: [ 'com' ] },
  subDomains: [ 'www' ],
  domain: 'example',
  topLevelDomains: [ 'com' ] }

Example 2

parseDomain(fromUrl("http://subsub.sub.test.ExAmPlE.coM/12xy45"))
{ type: 'LISTED',
  hostname: 'subsub.sub.test.example.com',
  labels: [ 'subsub', 'sub', 'test', 'example', 'com' ],
  icann:
   { subDomains: [ 'subsub', 'sub', 'test' ],
     domain: 'example',
     topLevelDomains: [ 'com' ] },
  subDomains: [ 'subsub', 'sub', 'test' ],
  domain: 'example',
  topLevelDomains: [ 'com' ] }

Why?

Depending on the use case and volume I strongly recommend against solving this problem yourself using regex or other string manipulation means. The core of this problem is that you need to know all the gtld and cctld suffixes to properly parse url strings into domain and subdomains, these suffixes are regularly updated. This is a solved problem and not one you want to solve yourself (unless you are google or something). Unless you need the hostname or domain name in a pinch don't try and parse your way out of this one.

Mighty answered 25/2, 2020 at 22:49 Comment(0)
F
0

A URL is schema://domain/path/to/resource?key=value#fragment so you could split on /:

/**
 * Get root of URL
 * @param {string} url - string to parse
 * @returns {string} url root or empty string
 */
function getUrlRoot(url) {
  return String(url || '').split('/').slice(0, 3).join('/');
}

Example:

getUrlRoot('http://www.youtube.com/watch?v=ClkQA2Lb_iE');
// returns http://www.youtube.com

getUrlRoot('http://youtu.be/ClkQA2Lb_iE');
// returns http://youtu.be

getUrlRoot('http://www.example.com/12xy45');
// returns http://www.example.com

getUrlRoot('http://example.com/random');
// returns http://example.com
Farra answered 16/2, 2023 at 1:19 Comment(0)
C
0

Simple :

const url = new URL("https://www.magicspoon.com/pages/miss-cereal-new-bday");
domainUrl = url.hostname?.split(".").slice(-2).join(".");
//domainUrl: magicspoon.com
--- 
const url = new URL("https://magicspoon.com/pages/miss-cereal-new-bday");
domainUrl = url.hostname?.split(".").slice(-2).join(".");
//domainUrl: magicspoon.com
Claribel answered 18/4, 2023 at 17:39 Comment(1)
Please add some explanation for your code rather than posting code only. Additional explanation will be more helpful.Armourer
M
-6

Try below code for exact domain name using regex,

String line = "http://www.youtube.com/watch?v=ClkQA2Lb_iE";

  String pattern3="([\\w\\W]\\.)+(.*)?(\\.[\\w]+)";

  Pattern r = Pattern.compile(pattern3);


  Matcher m = r.matcher(line);
  if (m.find( )) {

    System.out.println("Found value: " + m.group(2) );
  } else {
     System.out.println("NO MATCH");
  }
Misinterpret answered 25/4, 2016 at 10:11 Comment(1)
OP was looking for an answer in JavaScript, not Java.Resection

© 2022 - 2024 — McMap. All rights reserved.