»
EnglishFrenchVietnamese

Print - Simple JavaScript RegEx to Parse Domain Name - JavaScriptBank.com

Full version: jsB@nk » Utility » Generator » Simple JavaScript RegEx to Parse Domain Name
URL: https://www.javascriptbank.com/simple-javascript-regex-parse-domain-name.html

Simple JavaScript RegEx to Parse Domain Name © JavaScriptBank.comThere are many JavaScript resources to parse an URL in JavaScript RegEx. And parsing the domain main is easy since there's a lot of trick. This is useful for checking whether there is a third party domain involved in a page.Today, jsB@nk would like to present you another JavaScript example code to parse an URL address using JavaScript RegEx. This JavaScript object is built into 2 JavaScript public methods, we can use them to get domain root of an URL address, or check if whether domain is exist.Other JavaScript RegEx example codes you should try: - getElementsByClassName: Simple Shorthand Way to Get HTML Element by className with RegExp - JavaScript RegEx Example Code for Text Input Limitations - Built-in JavaScript RegEx APIs - RegExp Validation

Full version: jsB@nk » Utility » Generator » Simple JavaScript RegEx to Parse Domain Name
URL: https://www.javascriptbank.com/simple-javascript-regex-parse-domain-name.html



JavaScript
<script type="text/javascript">// Copyright: ivanceras - http://www.ivanceras.com//*     This script downloaded from www.JavaScriptBank.com     Come to view and download over 2000+ free javascript at www.JavaScriptBank.com*//** *  * USAGE: * UriParser.parseRootDomain("http://user:password@www.truste.com.ca:80/pathname?querystring&key=value#fragment"); //= truste.com.ca * UriParser.parseRootDomain("http://google.com.ph/pathname?querystring&key=value#fragment"); //= google.com.ph * UriParser.parseRootDomain("http://www.google.com/pathname?querystring&key=value#fragment"); //= google.com *  */var UriParser =  { //parse the root domain, exclude the sub domain parseRootDomain : function(url) {  var matches = url.match(/^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/);   //my additional code  var theDomain = matches[6];    if(UriParser.isIp(theDomain)){//if it is an ip address return it as domain   return theDomain;  }  var dots = theDomain.split('.');  var n = dots.length;    if(n < 3){//google.com, 2 root words concatenate, 1 word as well i.e. localhost   return dots.join(".");  }  else{//should be greater than or equal to 3 dot split ie. adsense.google.com   var last = dots[n-1];   var second2l = dots[n-2];   var third2l = dots[n-3];     var ext;   var root;   if(second2l.length <= 3){//if google.com.ph, or bbc.co.uk    ext = second2l +"."+ last;    root = third2l;   }else{// adsense.google.com    ext = last;    root = second2l;   }   var domain = ""+ root + "." + ext;   return domain;  } },  //private isNumber : function (o) {    return !isNaN (o-0); }, //private /**  * determines if the url is an ip address  */ isIp: function(domain){  var exploded = domain.split('.');  for(var i = 0; i < exploded.length; i++){   if(!UriParser.isNumber(exploded[i])){    return false;   }  }  return true; },  isSameDomain: function(url1, url2){  if(UriParser.parseRootDomain(url1) == UriParser.parseRootDomain(url2)){   return true;  }  return false; }}</script>


HTML
<h3>Usages:</h3>document.write(UriParser.parseRootDomain("http://user:password@www.truste.com.ca:80/pathname?querystring&key=value#fragment"));<br />document.write(UriParser.parseRootDomain("http://google.com.ph/pathname?querystring&key=value#fragment"));<br />document.write(UriParser.parseRootDomain("http://www.google.com/pathname?querystring&key=value#fragment"));<br />document.write(UriParser.parseRootDomain("http://google.com/pathname?querystring&key=value#fragment"));<br />document.write(UriParser.parseRootDomain("http://localhost/pathname?querystring&key=value#fragment"));<br />document.write(UriParser.parseRootDomain("localhost"));<br />document.write(UriParser.parseRootDomain("http://192.168.1.1/pathname?querystring&key=value#fragment"));<br />document.write(UriParser.isSameDomain("http://www.google.com/pathname?querystring&key=value#fragment", "http://adsense.google.com/?x=123123"))document.write(UriParser.isSameDomain("http://www.blogger.com/navbar.g?targetBlogID=5856931630868336061&blogName=TeamPilipinas.info&publishMode=PUBLISH_MODE_HOSTED&navbarType=BLACK&layoutType=LAYOUTS&searchRoot=http%3A%2F%2Fteampilipinas.info%2Fsearch&blogLocale=en_PH&homepageUrl=http%3A%2F%2Fteampilipinas.info%2F", "blogger.com"));<br /><hr /><h3>Results:</h3><script type="text/javascript">document.write(UriParser.parseRootDomain("http://user:password@www.truste.com.ca:80/pathname?querystring&key=value#fragment") + '<br />');document.write(UriParser.parseRootDomain("http://google.com.ph/pathname?querystring&key=value#fragment") + '<br />');document.write(UriParser.parseRootDomain("http://www.google.com/pathname?querystring&key=value#fragment") + '<br />');document.write(UriParser.parseRootDomain("http://google.com/pathname?querystring&key=value#fragment") + '<br />');document.write(UriParser.parseRootDomain("http://localhost/pathname?querystring&key=value#fragment") + '<br />');document.write(UriParser.parseRootDomain("localhost") + '<br />');document.write(UriParser.parseRootDomain("http://192.168.1.1/pathname?querystring&key=value#fragment") + '<br />');document.write(UriParser.isSameDomain("http://www.google.com/pathname?querystring&key=value#fragment", "http://adsense.google.com/?x=123123"))document.write(UriParser.isSameDomain("http://www.blogger.com/navbar.g?targetBlogID=5856931630868336061&blogName=TeamPilipinas.info&publishMode=PUBLISH_MODE_HOSTED&navbarType=BLACK&layoutType=LAYOUTS&searchRoot=http%3A%2F%2Fteampilipinas.info%2Fsearch&blogLocale=en_PH&homepageUrl=http%3A%2F%2Fteampilipinas.info%2F", "blogger.com"));</script>