Sunday, April 17, 2011

How can I strip the subdomain and .com/.org etc from a domain in Javascript?

I want to get just the domain name(yahoo, google, stackoverflow) from the domain in javascript. How can I easily pull just this out and drop any subdomain and any extension?

See comment, I need it to work for any domain if possible.

From stackoverflow
  • "www.google.com".match(/\.?([^.]+)\.[^.]+.?$/)[1]
    
  • var domain = 'http://www.google.com/';
    var parts = domain.toString().split('.'.toString());
    parts[parts.length - 2];
    
  • See how to get domain name from URL

0 comments:

Post a Comment