URL parser: parseUri (original) (raw)
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 andWHATWG URL.
Show examples
- https://a.b.site.co.uk:80/path/to/my.img.jpg?q=x&q=#hash
- ftp://user:pass@site.com/~user/file.txt
- //protocol-relative.url/🖤
- ssh://myid@192.168.1.101
- mailto:me@my.com?subject=Hey&body=Sign%20me%20up!
- https://[db8:85a3::7334]:80?q=x
- https://[db8:85a3::7334%en1]/ipv6-zone-identifier
- git://localhost:123
- file:///path/file
- file://host/path/file
- ../path/to/file.html?q=x
- example.com/file.html
- urn:isbn:0451450523
- tel:+1-800-555-0123
- http:\\\///site.com:0/web-protocol-edge-cases
- non-web-protocol:\\\///site.com:0/
- http:query-edge-cases??q=a&&?q&q=&=b
Options: Show differences from v1.2.2 Extend recognized top-level domains (ex: co.uk
)
Key | parseUri [default] | parseUri [friendly] | URL | URI.js |
---|