Created
November 20, 2011 04:52
-
-
Save mseeley/1379815 to your computer and use it in GitHub Desktop.
URL parsing regex
This file contains 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
(function () { | |
// Regexp is based on Crockford's "Javascript, the Good Parts" | |
// (?:([A-Za-z]+):)? - protocol (http, ftp, etc...) (optional) | |
// (\/{0,3}) - slash(es) | |
// (?:(\w+):(\w+)@)? - username:password@ (optional) -- does this need to support hyphens and dots? | |
// ([\w\-.]+) - host name (shortened from [0-9.\-A-Za-z]+) | |
// (?::(\d+))? - port number (optional) | |
// (?:\/([^?#]*))? - match path (optional) (caution, matches all character except ? and #, so it will also match line-ending characters, control character, and lots of other characters) | |
// (?:\?([^#]*))? - match querystring (optional) | |
// (?:#(.*))? - match fragment (optional) | |
var re = /^(?:([A-Za-z]+):)?(\/{0,3})(?:(\w+):(\w+)@)?([\w\-.]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/, | |
names = ["href", "protocol", "slash", "username", "password", "host", "port", "pathname", "search", "hash"], | |
len = names.length, | |
cache = {}, | |
url = { | |
// Always return at least an object w/ empty values. | |
parse: function (href) { | |
if (cache[href]) | |
return cache[href]; | |
var values = href ? re.exec(href) : [], | |
result = {}, | |
count = len; | |
// Skip 'href' value in index 0 | |
while (--count) | |
result[names[count]] = values[count] || ""; | |
cache[href] = result; | |
return result; | |
}, | |
// https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript | |
isSameOrigin: function (hrefA, hrefB) { | |
var parse = url.parse, | |
a = parse(hrefA), | |
b = parse(hrefB); | |
// FIXME: Handle empty responses from parse() | |
return a.protocol == b.protocol && | |
a.host == b.host && | |
a.port == b.port; | |
} | |
}; | |
// Expose public interface | |
window.url = url; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment