Simple Javascript regex to find root domain

I have a function that uses regex to return the root domain of a given URL.

http://jsfiddle.net/hSpsT/

function cleanUp(url) {
  url = url.replace(new RegExp(/^\s+/),""); // START
  url = url.replace(new RegExp(/\s+$/),""); // END

  // IF FOUND, CONVERT BACK SLASHES TO FORWARD SLASHES
  url = url.replace(new RegExp(/\\/g),"/");

  // IF THERE, REMOVES 'http://', 'https://' or 'ftp://' FROM THE START
  url = url.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i),"");

  // IF THERE, REMOVES 'www.' FROM THE START OF THE STRING
  url = url.replace(new RegExp(/^www\./i),"");
  //remove slash from end
  url = url.replace(new RegExp(/\/$/i),"");    
  return url;
}

      

But it uses a multi-mode expression and we are concerned about performance. Is there a better way to do the same in a single line regex?

Note:

document.location.host doesn't seem to work in my case.

+3


source to share


2 answers


Extract hostname from string

Try:



function cleanUp(url) {
    var url = $.trim(url);
    if(url.search(/^https?\:\/\//) != -1)
        url = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i, "");
    else
        url = url.match(/^([^\/?#]+)(?:[\/?#]|$)/i, "");
    return url[1];
}

alert(cleanUp('  http://www.google.com/about.html'));
alert(cleanUp('  www.google.com/about.html'));

      

+5


source


Try the following:

http://jsfiddle.net/picklespy/gb34u/1/



It works on all modern browsers and even IE 5.5+.

var url = document.createElement('a');
url.href = 'http://maps.test.google.com';
var host = url.hostname;

host = host.split('.');

var domain = host.pop();
domain = host.pop() + '.' + domain;

alert('Root is: ' + domain)

      

+1


source







All Articles