title: Regular Expression - Extract subdomain & domain
body: <p>I'm trying to form a regular expression (javascript/node.js) which will extract the sub-domain &amp; domain part from any given URL. This is what I ended up with:</p>
  
  <pre><code>[^(?:http:\/\/|www\.|https:\/\/)]([^\/]+)
  </code></pre>
  
  <p>Right now, I'm just considering http, https for protocol &amp; exclude "www." portion from the subdomain+domain portion of an URL. I checked the expression &amp; it almost works. But, here is the issue:</p>
  
  <p><strong>Success</strong></p>
  
  <pre><code>'http://mplay.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http:\/\/|www\.|https:\/\/)]([^\/]+)/i)
  
  'http://lplay.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http:\/\/|www\.|https:\/\/)]([^\/]+)/i)
  </code></pre>
  
  <p><strong>Failure</strong></p>
  
  <pre><code>'http://play.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http:\/\/|www\.|https:\/\/)]([^\/]+)/i)
  
  'http://tplay.google.co.in/sadfask/asdkfals?dk=10'.match(/[^(?:http:\/\/|www\.|https:\/\/)]([^\/]+)/i)
  </code></pre>
  
  <p>I just use the first element from the result array. I'm not able to understand why "play." &amp; "tplay." doesn't work. Could anyone please help me in this regard? </p>
  
  <p>Does "/p" and "/t" have any meaning for the regular expression evaluator?</p>
  
  <p>Is there any other way of extracting sub-domain &amp; domain from any given URL using a regular expression?</p>
  
  <p>Edit -</p>
  
  <p>Example:</p>
  
  <p><a href="https://play.google.com/store/apps/details?id=com.skgames.trafficracer">https://play.google.com/store/apps/details?id=com.skgames.trafficracer</a> => <strong>play.google.com</strong></p>
  
  <p><a href="https://mail.google.com/mail/u/0/#inbox">https://mail.google.com/mail/u/0/#inbox</a> => <strong>mail.google.com</strong></p>