parseUri 1.2: Split URLs in JavaScript

I've just updated parseUri. If you haven't seen the older version, parseUri is a function which splits any well-formed URI into its parts, all of which are optional. Its combination of accuracy, flexibility, and brevity is unrivaled.

Highlights:

  • Comprehensively splits URIs, including splitting the query string into key/value pairs. (Enhanced)
  • Two parsing modes: loose and strict. (New)
  • Easy to use (returns an object, so you can do, e.g., parseUri(uri).anchor).
  • Offers convenient, pre-concatenated components (path = directory and file; authority = userInfo, host, and port; etc.)
  • Change the default names of URI parts without editing the function, by updating parseUri.options.key. (New)
  • Exceptionally lightweight (1 KB before minification or gzipping).
  • Released under the MIT License.

Details:

Older versions of this function used what's now called loose parsing mode (which is still the default in this version). Loose mode deviates slightly from the official generic URI spec (RFC 3986), but by doing so allows the function to split URIs in a way that most end users would expect intuitively. However, the finer details of loose mode preclude it from properly handling relative paths which do not start from root (e.g., "../file.html" or "dir/file.html"). On the other hand, strict mode attempts to split URIs according to RFC 3986. Specifically, in loose mode, directories don't need to end with a slash (e.g., the "dir" in "/dir?query" is treated as a directory rather than a file name), and the URI can start with an authority without being preceded by "//" (which means that the "yahoo.com" in "yahoo.com/search/" is treated as the host, rather than part of the directory path).

Since I've assumed that most developers will consistently want to use one mode or the other, the parsing mode is not specified as an argument when running parseUri, but rather as a property of the parseUri function itself. Simply run the following line of code to switch to strict mode:

parseUri.options.strictMode = true;

From that point forward, parseUri will work in strict mode (until you turn it back off).

The code:

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

You can download it here.

parseUri has no dependencies, and has been tested in IE 5.5–7, Firefox 2.0.0.4, Opera 9.21, Safari 3.0.1 beta for Windows, and Swift 0.2.

173 thoughts on “parseUri 1.2: Split URLs in JavaScript”

  1. The home inspection document also allows you having a great
    deal of advantage during price negotiations.

    Your home decor stores are filled with several special decorating accessories.

  2. What’s created is more important than any one of states they say you will
    get in the broker or the seller. Many parents, like me, usually find achievement by converting on an educational plan or
    an educational DVD.

  3. Begin In plan view to view your design in
    2D from above, or switch to 3D view. It could be overwhelming to first time homebuyers.
    Some affiliate programs pay as much as 75% commission
    to the affiliates.

  4. Almost all business start-ups require some kind of funding.
    There are many different ways that they VoIP is fast becoming the option of businesses everywhere.

    Offer both types- cordless and with cord too.

  5. What’s up it’s me, I am also visiting this website on a regular basis, this
    website is really nice and the users are really sharing good
    thoughts.

  6. The common revenue for someone using a degree is $52,000.
    A lot of them have all the basic amenities available. Form
    is one allergen that’s to not be taken lightly. Likewise, do not only expect a 3D movie.

  7. I do believe all of the concepts you have offered to your post.
    They’re really convincing and will definitely work.
    Nonetheless, the posts are very quick for novices.

    May just you please prolong them a bit from next time?
    Thanks for the post.

  8. Another problem is that Printopia is not free. A lot of people need to know where you can get an iPad 2 today?
    Each section of iWork is just a separate purchase. Typically
    there’s multiple solution to jailbreak.

  9. In adding ? , it appear to work

    ^(?:(?![^:@?]+:[^:@?\/]*@?)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@?]*)(?::([^:@?]*))?)?@?)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)

  10. Clicking on the link in the article above, labelled “Try the demo” results in the following error;
    Error occurred: 403 – forbidden
    Apache Server at: s1075064.instanturl.net

  11. Hello Steven,
    We were using this script and it really works well.
    However, recently, we ran into an issue, if we use IPv6 URL [like http://%5B2001:ae::52]/index.html?query=value ]
    The script ends up with incorrect parameter values for file, host, path etc.

    So, I was wondering if there is a newer version which handles IPv6 URIs as well?

    Thanks in advance.

    Thanks,
    Vibhav

  12. I wish it had the same property names as are available on window.location or in the URL class:
    hash, search, href, origin, pathname

Leave a Reply

Your email address will not be published. Required fields are marked *