Created
November 8, 2012 15:28
-
-
Save MathRobin/4039470 to your computer and use it in GitHub Desktop.
URL parser
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Mathieu ROBIN <http://www.mathieurobin.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
/** | |
* Parse URL to get information | |
* | |
* @param url the URL string to parse | |
* @return parsed the URL parsed or null | |
*/ | |
var UrlParser = function (url) { | |
"use strict"; | |
var regx = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/, | |
matches = regx.exec(url), | |
parser = null; | |
if (null !== matches) { | |
parser = { | |
href : matches[0], | |
withoutHash : matches[1], | |
url : matches[2], | |
origin : matches[3], | |
protocol : matches[4], | |
protocolseparator : matches[5], | |
credhost : matches[6], | |
cred : matches[7], | |
user : matches[8], | |
pass : matches[9], | |
host : matches[10], | |
hostname : matches[11], | |
port : matches[12], | |
pathname : matches[13], | |
segment1 : matches[14], | |
segment2 : matches[15], | |
search : matches[16], | |
hash : matches[17] | |
}; | |
} | |
return parser; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment