Javascript simple regex to find root domain - Stack Overflow

I have a function that uses regex to return root domain of the given url.function cleanUp(url) {url =

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

/

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.

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

http://jsfiddle/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.

Share Improve this question asked Feb 11, 2013 at 7:43 SelvamSelvam 3032 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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./about.html'));
alert(cleanUp('  www.google./about.html'));

Try this:

http://jsfiddle/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.';
var host = url.hostname;

host = host.split('.');

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

alert('Root is: ' + domain)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744036268a4547475.html

相关推荐

  • Javascript simple regex to find root domain - Stack Overflow

    I have a function that uses regex to return root domain of the given url.function cleanUp(url) {url =

    9天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信