Encode a URL component strictly
Percent-encode a value, including !'()* that encodeURIComponent leaves alone.
function encodeStrict(value) { return encodeURIComponent(value).replace(/[!'()*]/g, char => `%${char.charCodeAt(0).toString(16).toUpperCase()}`); }
Input & expected output
Input
"it's (50%) off!"Expected output
"it%27s%20%2850%25%29%20off%21"The edge case
Input
"~-_."Expected output
"~-_."Know the limits
- RFC 3986 unreserved characters (- _ . ~) stay as they are, as the edge case shows. The original also encoded ~, which RFC 3986 leaves unreserved.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Encode a URL from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Encodes !'()* with one replacement and leaves RFC 3986 unreserved ~ as is. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Convert Celsius to Fahrenheit — Apply the temperature conversion without rounding the result.
- Convert hex to RGB — Expand #rgb if needed and read the red, green and blue channels.
- Convert URL parameters to object — Read a query string into an object; repeated keys become arrays.