Mark Forums Read
  #1  
Old 06-17-2011, 03:01 AM
JavaScriptBank JavaScriptBank is offline
Senior Member
 
Join Date: Sep 2009
Posts: 130
JavaScriptBank is on a distinguished road
Default Simple JavaScript RegEx to Parse Domain Name

There 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 f... detail at JavaScriptBank.com - 2.000+ free JavaScript codes


How to setup

Step 1: Use JavaScript code below to setup the script
JavaScript
Code:
<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>
Step 2: Copy & Paste HTML code below in your BODY section
HTML
Code:
<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>





Reply With Quote
 #Add to Ads's Reputation  
OldSponsored Ads
Ads AdsPromoter is online
Member
 
Join Date: LongTime
Posts: 1100
Ads is on a distinguished road
Default New Sponsored Ads



This message will go away once you are registered. Also, by registering, you will have access to all post topics, communicate privately with other members (PM), respond to polls, upload graphics, and access other special features! Registration is fast, simple and absolutely free so please Click Here to join our Web Hosting community today!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 06:36 PM.


Powered by vBulletin Version 3.6.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 2.4.0