Created
November 12, 2012 03:28
-
-
Save voodooGQ/4057330 to your computer and use it in GitHub Desktop.
REGEX: Parse URL into individual parts (JavaScript: The Good Parts)
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
REGEX: Parse URL into individual parts (Javascript: The Good Parts) | |
<script type="text/javascript" language="javascript"> | |
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/; | |
var url= "http://www.ora.com:80/goodparts?q#fragment"; | |
var results = parse_url.exec(url); | |
var names = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash']; | |
var blanks = ' '; | |
var i; | |
for (i = 0; i < names.length; i += 1){ | |
document.writeln('<strong>' + names[i] + '</strong>: ' + | |
blanks.substring(names[i].length), results[i] + "<br />"); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This regular expression has a good balance between robustness and relative simplicity.
It needs to handle user:password however.