parseUri GitHub // URL parser

parseUri is a mighty but tiny (1KB) JavaScript URI/URN/URL parser that splits any URI into its parts (all of which are optional). Its combination of accuracy, comprehensiveness, and brevity is unrivaled. This demo page allows you to test its results and compare with JavaScript’s built-in URL constructor and URI.js (a much larger library).

Modes: parseUri has two parsing modes: default and friendly. Usually they produce the same result, but the default mode follows official URI standards and friendly mode allows handling human-friendly URLs like 'example.com/file.html' as expected. More specifically, friendly mode doesn’t require <protocol>:, :, //, or other repeating slashes to signal the start of an authority, but this also precludes it from properly handling relative paths (no leading / or \) such as 'dir/file.html'.

Show more details

Normalization: Unlike JavaScript’s built-in URL constructor and some other libraries, parseUri doesn’t normalize or encode results (normalization often differs between libraries and might or might not include encoding). In the results table, values that match parseUri in default mode after applying a library’s own normalization to the full URI are shown in light green.

Results: parseUri makes a best case effort even with partial or invalid URIs. Compare this to JavaScript’s built-in URL constructor which throws e.g. if not given a protocol, and in many other cases of valid (but not supported) and invalid URIs. URI.js throws with some but not all invalid URIs (e.g. it throws if given port 0, which is in fact valid with UDP).

Here’s an example of what each part contains:

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ href │ ├────────────────────────────────────────────────────────────────┬─────────────────────────────────────────┤ │ origin │ resource │ ├──────────┬─┬───────────────────────────────────────────────────┼──────────────────────┬───────┬──────────┤ │ protocol │ │ authority │ pathname │ query │ fragment │ │ │ ├─────────────────────┬─────────────────────────────┼───────────┬──────────┤ │ │ │ │ │ userinfo │ host │ directory │ filename │ │ │ │ │ ├──────────┬──────────┼──────────────────────┬──────┤ ├─┬────────┤ │ │ │ │ │ username │ password │ hostname │ port │ │ │ suffix │ │ │ │ │ │ │ ├───────────┬──────────┤ │ │ ├────────┤ │ │ │ │ │ │ │ subdomain │ domain │ │ │ │ │ │ │ │ │ │ │ │ ├────┬─────┤ │ │ │ │ │ │ │ │ │ │ │ │ │ tld │ │ │ │ │ │ │ " https :// user : pass @ sub1.sub2 . dom.com : 8080 /p/a/t/h/ a.html ? q=1 # hash " └──────────────────────────────────────────────────────────────────────────────────────────────────────────┘

parseUri additionally supports IPv4 and IPv6 addresses, URNs, and many edge cases not shown here. See the extensive tests. References include RFC 3986 and WHATWG URL.

Show examples

Options:

Key parseUri [default]