Check if a url contains a subdomain


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function subDomain(url) {
  2.  
  3. // IF THERE, REMOVE WHITE SPACE FROM BOTH ENDS
  4. url = url.replace(new RegExp(/^\s+/),""); // START
  5. url = url.replace(new RegExp(/\s+$/),""); // END
  6.  
  7. // IF FOUND, CONVERT BACK SLASHES TO FORWARD SLASHES
  8. url = url.replace(new RegExp(/\\/g),"/");
  9.  
  10. // IF THERE, REMOVES 'http://', 'https://' or 'ftp://' FROM THE START
  11. url = url.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i),"");
  12.  
  13. // IF THERE, REMOVES 'www.' FROM THE START OF THE STRING
  14. url = url.replace(new RegExp(/^www\./i),"");
  15.  
  16. // REMOVE COMPLETE STRING FROM FIRST FORWARD SLASH ON
  17. url = url.replace(new RegExp(/\/(.*)/),"");
  18.  
  19. // REMOVES '.??.??' OR '.???.??' FROM END - e.g. '.CO.UK', '.COM.AU'
  20. if (url.match(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i))) {
  21. url = url.replace(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i),"");
  22.  
  23. // REMOVES '.??' or '.???' or '.????' FROM END - e.g. '.US', '.COM', '.INFO'
  24. } else if (url.match(new RegExp(/\.[a-z]{2,4}$/i))) {
  25. url = url.replace(new RegExp(/\.[a-z]{2,4}$/i),"");
  26. }
  27.  
  28. // CHECK TO SEE IF THERE IS A DOT '.' LEFT IN THE STRING
  29. var subDomain = (url.match(new RegExp(/\./g))) ? true : false;
  30.  
  31. return(subDomain);
  32.  
  33. }

URL: http://www.web-wise-wizard.com/javascript-tutorials/javascript-regular-expressions-regexp.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.