3

I have a function that uses regex to return root domain of the 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 multi regex and we are worried about the performance. Is there a better way to do the same in a one line regex?

Note:

document.location.host does not seem to work in my case.

Selvam
  • 193
  • 1
  • 13

2 Answers2

5

Extract hostname name 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'));
Community
  • 1
  • 1
geniuscarrier
  • 4,199
  • 2
  • 21
  • 15
  • I have updated it for www.google.com, http://jsfiddle.net/svjTC/1/ , it might fail in that case, can we extend it ? – Selvam Feb 12 '13 at 05:07
  • Awesome. Added this to end url[1] = url[1].replace(/^www\./i, ""); to remove www. Looks fine to me. Thanks a lot. – Selvam Feb 13 '13 at 05:50
  • thanks but trim seems to internally use regex, which is additional overhead. Was looking for it to happen in existing regex. – Selvam Feb 14 '13 at 05:26
  • @Selvam In terms of trim with regex, here is an awesome article doing efficiency analysis: [link](http://blog.stevenlevithan.com/archives/faster-trim-javascript). Also, for common javascript regex, please see this: [link](http://www.geniuscarrier.com/common-regular-expressions-in-javascript/) – geniuscarrier Feb 14 '13 at 18:39
1

Try this:

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

It works on all modern browsers and even on 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)
web-nomad
  • 6,003
  • 3
  • 34
  • 49