1

With the help from this answer I'm getting clean domain names from urls in strings, like the following:

url = "http://www.stackoverflow.com/questions/ask"  
var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);  
return matches ? matches[1] : url;
>> "www.stackoverflow.com"

I would like to remove the subdomain "www" (and the following dot) as well though, if existing. How would I change the above expression to accomplish this?

Community
  • 1
  • 1
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
  • A simple `.replace("www.","")` would work just fine. – Jorge Campos Jan 05 '14 at 14:35
  • Maybe this helps: http://stackoverflow.com/questions/6738752/regex-for-dropping-http-and-www-from-urls – Xar Jan 05 '14 at 14:37
  • 4
    @JorgeCampos That would cause trouble when the string "www." appears in a different context. Like in `http://example.com/mythoughtsaboutthewww.html` – Philipp Jan 05 '14 at 14:38

2 Answers2

2

You can match optional www. after http://:

var matches = url.match(/^https?\:\/\/(?:www\.)?([^\/?#]+)(?:[\/?#]|$)/i);
//=> ["http://www.stackoverflow.com/", "stackoverflow.com"]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

You can try,

url = "http://www.stackoverflow.com/questions/ask"  
var matches = url.match(/^https?\:\/\/(www\.)?([^\/?#]+)(?:[\/?#]|$)/i);  
return matches ? matches[2] : url;

to support url addresses with and without www.

melc
  • 11,523
  • 3
  • 36
  • 41