-2

I want to get the domain name without www

ex: https://www.gmail.com/anything Output should be gmail.com(or .net or .org)

Can anyone help me in giving a regex for this?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Kranthi
  • 1,377
  • 1
  • 17
  • 34

2 Answers2

4

Use a regex such as /(?:https?:\/\/)?(?:www\.)?(.*?)\//:

var str = "https://www.gmail.com/anything";
var match = str.match(/(?:https?:\/\/)?(?:www\.)?(.*?)\//);
console.log(match[match.length-1]); //gmail.com (last group of the match)

Note: This will get everything after the http/https protocol, not including www - up to the first slash.

Extra note: A lot of domains use sub domains - hence mail.google.com would suddenly become google.com and hence not work. Mine includes every subdomain apart from www.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • thanks and its working and the issue is i may or may not get https ex: I may get www.gmail.com also. So how can i change that to work? – Kranthi Jul 02 '13 at 14:07
  • @kittuss By using questionmarks. `https?:\/\/` means "the string `http`, followed by an optional `s`, followed by `://`". Hence `(https?:\/\/)?` means the previous, but the question mark means "repeated 1 or 0 times" (so it's optional). Check my updated answer for working code. When a parenthesis starts with `?:` it's a non-capturing group, meaning "do not put this match into our match results". – h2ooooooo Jul 02 '13 at 14:17
  • match variable is null when i put this code in input blur function in jquery.. Whats wrong? http://jsfiddle.net/b33kafq4/ – sstauross Oct 30 '14 at 13:26
  • @sstauross You need to have something after the domain (it requires `/` in the end). `http://www.google.com` = `http:`. `http://www.google.com/` = `google.com`. – h2ooooooo Oct 30 '14 at 13:29
  • what if i don't want to have this limitation? – sstauross Oct 30 '14 at 13:34
  • @sstauross Then make it optional with `?` or use the below solution by creating an `a` tag and getting the hostname off that. – h2ooooooo Oct 30 '14 at 13:35
  • ok i understand the solution below but what about the regex? what if i completely remove the \/ like this:http://jsfiddle.net/b33kafq4/2/ whats wrong with that? i want to get whatever is after the http://,https://,www. , could you help with this? – sstauross Oct 30 '14 at 14:10
  • `^https?:\/\/(?:www\.)?([^\/]+)`. – h2ooooooo Oct 30 '14 at 14:23
  • That is ok, but I suggest to add `\?` at the end, otherwise it would give `null` when domain string ends without `\`. – loretoparisi Apr 06 '16 at 13:46
3

You can use a <a> to get information about a URL. For example:

var a = document.createElement("a");
a.href = "http://www.google.com";

You can retrieve the domain with:

var domain = a.hostname;

And you can strip away any leading "www.":

domain = domain.replace(/^www\./, "");

As a reusable function, you could use:

function getDomain(url) {
    var a, domain;

    a = document.createElement("a");
    a.href = url;

    domain = a.hostname;
    domain = domain.replace(/^www\./, "");

    return domain;
}

DEMO: http://jsfiddle.net/DuK6D/


More info/attributes about the HTMLAnchorElement JS object on MDN

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
Ian
  • 50,146
  • 13
  • 101
  • 111