URL decode and encode

Turn %C3%A9 back into é, or make any text safe to put in a URL. Pick the right encoding for a query value, a full URL or a form body, and see the query string parsed.

protocol https:host regexkit.devpath /searchhash #top

ParameterValue (decoded)
qcafé & crêpes
tagsa,b

Which encoding to use

Building ?q= from user input? Encode just the value with Component (encodeURIComponent). Cleaning up a full URL that has spaces or accents in it? Use Full URL (encodeURI), which keeps / ? & = # intact. Sending a form body or reading one? Form turns spaces into +, like URLSearchParams does.

CharacterComponentFull URLForm
space%20%20+ (forms)
&%26&%26
=%3D=%3D
?%3F?%3F
/%2F/%2F
#%23#%23
+%2B+%2B
%%25%25%25
é%C3%A9%C3%A9%C3%A9
✓%E2%9C%93%E2%9C%93%E2%9C%93

In code

// JavaScript
const url = `https://api.example.com/search?q=${encodeURIComponent(query)}`;
new URLSearchParams({ q: 'café & crêpes' }).toString();   // q=caf%C3%A9+%26+cr%C3%AApes

# Python
from urllib.parse import quote, unquote, urlencode
quote('café & crêpes', safe='')    # caf%C3%A9%20%26%20cr%C3%AApes
urlencode({'q': 'café & crêpes'})  # q=caf%C3%A9+%26+cr%C3%AApes

The rules come from RFC 3986, which lists the characters that may appear unescaped in a URI; everything else is written as a percent sign plus two hex digits per UTF-8 byte.

Questions

What is the difference between encodeURIComponent and encodeURI?

encodeURIComponent escapes everything except letters, digits and - _ . ! ~ * ' ( ), so it is right for a single query value or path segment. encodeURI leaves URL structure characters (: / ? # & = +) alone and is for tidying a whole URL. Using encodeURI on a value that contains & or = breaks the query string.

Should a space be %20 or +?

In paths and generic URLs, %20. The + form only means a space in application/x-www-form-urlencoded data — HTML form submissions and most query strings. Choose "Form" mode to use it.

Why do I see %25 everywhere?

%25 is an encoded percent sign — your text was encoded twice. Decode it twice, and fix the code that encodes an already-encoded value.

How are non-English characters encoded?

As their UTF-8 bytes, each written as %XX. é is two bytes (%C3%A9); most emoji are four.