title: javascript:How to extract country code from url using regex
body: <p>there is country code in url which i need to extract by regex and javascript.</p>
  
  <h2>so my possible url would be</h2>
  
  <p><strong><a href="http://example.com/gb/index.aspx" rel="nofollow noreferrer">http://example.com/gb/index.aspx</a></strong></p>
  
  <p><strong><a href="http://localhost:2020/gb/index.aspx" rel="nofollow noreferrer">http://localhost:2020/gb/index.aspx</a></strong></p>
  
  <p><strong><a href="http://example:2020/gb/index.aspx" rel="nofollow noreferrer">http://example:2020/gb/index.aspx</a></strong></p>
  
  <p>these code i tried but regex works for a specific type of url.</p>
  
  <pre><code>var url = "http://example.com/gb/index.aspx";
  //Get the language code
  var countrycode = /com\/([^\/]+)/.exec(url)[1];
  </code></pre>
  
  <p>the above code works when url look like <strong><a href="http://example.com/gb/index.aspx" rel="nofollow noreferrer">http://example.com/gb/index.aspx</a></strong> but the moment url look like <strong><a href="http://localhost:2020/gb/index.aspx" rel="nofollow noreferrer">http://localhost:2020/gb/index.aspx</a></strong> or <strong><a href="http://example:2020/gb/index.aspx" rel="nofollow noreferrer">http://example:2020/gb/index.aspx</a></strong> then above code does not works. so tell me which regex i need to use which can extract country code from above 3 different kind of url. need some hint. thanks</p>