title: How to get domain from a string using javascript regular expression
body: <p>As the title suggests, I'm trying to retrieve the domain from a string using javascript regular expression.</p>
  
  <p>Take the following strings: </p>
  
  <pre><code>String                                  ==&gt;     Return
  "google"                                ==&gt;     null
  "google.com"                            ==&gt;     "google.com"
  "www.google.com"                        ==&gt;     "www.google.com"
  "ftp://ftp.google.com"                  ==&gt;     "ftp.google.com"
  "http://www.google.com"                 ==&gt;     "www.google.com"
  "http://www.google.com/"                ==&gt;     "www.google.com"
  "https://www.google.com/"               ==&gt;     "www.google.com"
  "https://www.google.com.sg/"            ==&gt;     "www.google.com.sg"
  "https://www.google.com.sg/search/"     ==&gt;     "www.google.com.sg"
  "*://www.google.com.sg/search/"         ==&gt;     "www.google.com.sg"
  </code></pre>
  
  <p>I've already read    <a href="https://stackoverflow.com/questions/17427603/regex-to-find-domain-name-without-www">"Regex to find domain name without www - Stack Overflow"</a> and <a href="https://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string">"Extract root domain name from string - Stack Overflow"</a>    but they were too complicated so I tried writing my own regular expression: </p>
  
  <pre><code>var re = new RegExp("[\\w]+[\\.\\w]+");
  /[\w]+[\.\w]+/
  re.exec(document.URL);
  </code></pre>
  
  <p>which works fine with <code>"google.com"</code>, <code>"www.google.com"</code> and <code>"www.google.com.sg"</code> but returns <code>http</code> with <code>"http://google.com/"</code>, <code>"http://www.google.com/"</code> etc.</p>
  
  <p>As I am new  to regular expressions, I can't seem to figure out what's wrong... any ideas?</p>
  
  <p>Thanks in advance!</p>