parseUri
Context:
parseUri is a mighty but tiny 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.
Demo:
This demo page is for testing parseUri and comparing its results with v1, the
built-in URL
constructor, and URI.js (a much larger
library).
Parsing modes:
parseUri has two parsing modes: default and friendly. The default mode follows
official URI rules. Friendly mode doesn’t require '<protocol>:',
':', or '//' to signal the start of an authority, which allows
handling human-friendly URLs like 'example.com/file.html' as expected. However,
it also precludes friendly mode from properly handling relative paths (that don’t start from
root '/') such as 'dir/file.html'.
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 tests. References include
RFC 3986 and the
WHATWG URL Standard.
Options:
| Key | parseUri [default] |
|---|