Parse and Decode Percent-Encoded URLs
Understand URL percent-encoding (URL encoding), how special characters are escaped in URLs, when to use encodeURI vs encodeURIComponent, and how to correctly decode encoded URLs.
Detailed Explanation
URL Percent-Encoding
Percent-encoding (also called URL encoding) replaces unsafe characters with a % followed by two hexadecimal digits representing the character's byte value. This ensures URLs only contain valid ASCII characters.
How It Works
Original: https://example.com/search?q=hello world&lang=日本語
Encoded: https://example.com/search?q=hello%20world&lang=%E6%97%A5%E6%9C%AC%E8%AA%9E
Reserved vs Unreserved Characters
Unreserved characters (never need encoding):
A-Z a-z 0-9 - _ . ~
Reserved characters (have special meaning in URLs):
: / ? # [ ] @ ! $ & ' ( ) * + , ; =
These should only be encoded when used as data values, not when serving their syntactic purpose.
JavaScript Encoding Functions
| Function | Encodes | Does NOT encode |
|---|---|---|
encodeURI() |
Spaces, non-ASCII | : / ? # [ ] @ ! $ & ' ( ) * + , ; = |
encodeURIComponent() |
Everything except | A-Z a-z 0-9 - _ . ~ ! ' ( ) * |
encodeURI("https://example.com/path?q=hello world")
// "https://example.com/path?q=hello%20world"
encodeURIComponent("hello world & goodbye")
// "hello%20world%20%26%20goodbye"
When to Use Which
encodeURI()— When encoding a full URL (preserves URL structure)encodeURIComponent()— When encoding a single parameter valueURLSearchParams— Automatically handles encoding for query parameters
Common Encoding Pitfalls
- Double encoding — Encoding an already-encoded URL produces
%2520instead of%20 - Plus vs %20 — In query strings,
+means space. In paths, it means a literal plus. - UTF-8 multibyte — Non-ASCII characters produce multiple
%XXsequences - Decoding mismatch — Using
decodeURI()on a component-encoded string can fail
Decoding
decodeURI("https://example.com/path%20with%20spaces")
// "https://example.com/path with spaces"
decodeURIComponent("hello%20world%20%26%20goodbye")
// "hello world & goodbye"
Use Case
URL encoding is critical when building API requests, constructing redirect URLs, handling user-generated content in URLs, and working with internationalized domain names. Mishandling encoding is a common source of bugs — broken links, failed API calls, and even security vulnerabilities like open redirects can result from incorrect URL encoding.