Created
October 21, 2011 20:47
-
-
Save ArthurN/1304914 to your computer and use it in GitHub Desktop.
Parsing Domain Names ("providers")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from: | |
// http://www.webtalkforums.com/showthread.php/37600-Simple-JavaScript-RegEx-to-Parse-Domain-Name | |
var parts = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(item['url']) | |
if (parts != null) { | |
providerUrl = parts[1] + parts[6]; | |
providerName = parts[6].replace("www.", ""); | |
} else { | |
// Graceful fail | |
//console.debug("Failed URL parsing on: " + item['url']); | |
providerUrl = item['url']; | |
providerName = providerUrl.replace('http://', '').replace('https://', '').replace('www.', ''); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parse default Provider Name and URL from url | |
def get_default_link_provider( url ) | |
parts = url.split( /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/ ) | |
if parts && parts.length >= 4 | |
# Remove "www." from "www.provider_name.com" | |
provider_name = parts[3].gsub( /www./, "" ) | |
# Concatenate "http://" and "www.provider_name.com" | |
provider_url = parts[1] + parts[3] | |
else | |
# Fail gracefully | |
provider_url = url | |
provider_name = url.gsub( /http:\/\//, '' ).gsub( /https:\/\//, '' ).gsub( /www./, '' ) | |
end | |
{ :provider_name => provider_name, :provider_url => provider_url } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment