Using Regex to find domain name without www - Javascript String Operation

Javascript examples for String Operation:String Search

Description

Using Regex to find domain name without www

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//w w  w  .j a  va 2 s . c  om
function getDomain(url) {
    var a, domain;
    a = document.createElement("a");
    a.href = url;
    domain = a.hostname;
    domain = domain.replace(/^www\./, "");
    return domain;
}
console.log(getDomain("http://www.gmail.com"));
console.log(getDomain("https://gmail.com"));
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials