Encode a URL component strictly

Percent-encode a value, including !'()* that encodeURIComponent leaves alone.

JavaScript Version 1.0.0 · MIT · .md

encode-a-url.js
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.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.