The problem with the above expression: if you do not know what the protocol is, or what the domain suffix is, you will get unexpected results. Here are some regular expressions for these situations: D
/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i //javascript
This should always return your subdomain (if any) in group 1. Here it is shown in the Javascript example, but it should also work for any other engine that supports positive forward statements:
// EXAMPLE of use var regex = /(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i , whoKnowsWhatItCouldBe = [ "www.mydomain.com/whatever/my-site" //matches: www , "mydomain.com"// does not match , "http://mydomain.com" // does not match , "https://mydomain.com"// does not match , "banana.com/somethingelse" // does not match , "https://banana.com/somethingelse.org" // does not match , "http://what-ever.mydomain.mu" //matches: what-ever , "dev-www.thisdomain.com/whatever" // matches: dev-www , "hot-MamaSitas.SomE_doma-in.au.xxx"//matches: hot-MamaSitas , "http://hot-MamaSitas.SomE_doma-in.au.xxx" // matches: hot-MamaSitas , "..ru" //even non english chars! Woohoo! matches: , ".ru" //does not match ]; // Run a loop and test it out. for ( var i = 0, length = whoKnowsWhatItCouldBe.length; i < length; i++ ){ var result = whoKnowsWhatItCouldBe[i].match(regex); if(result != null){ // YAY! We have a match! } else { // Boo... No subdomain was found } }
source share