What is string escaping?
Escaping means rewriting the characters that have a special meaning in a format so that they are read as plain text. A double quote inside a JSON string must become \", a less-than sign in HTML must become <, a space in a URL must become %20, and a single quote in a SQL string must be doubled. Unescaping is the reverse: turning the escaped form back into the original text. Getting it wrong causes broken JSON, garbled pages, failed imports and, in the worst case, injection vulnerabilities.
This string escape and unescape tool covers the ten formats developers meet most: JSON, JavaScript, HTML, XML, URL (percent-encoding), SQL, CSV, regular expressions, the shell (bash and zsh) and C. You paste text, choose the format (or let the tool detect it) and get the result with a copy button. There are options for what changes between variants: writing non-ASCII characters as \uXXXX, the kind of quote around the string, the SQL dialect (standard with doubled quotes or MySQL with backslashes), the CSV delimiter, and single or double quotes for the shell. When you unescape text that is not valid, the error points to the line and column and says what is wrong. Everything runs in your browser, so tokens, queries and customer data you paste are never sent anywhere.
How to escape and unescape a string
- Paste or type the text. A sample with quotes, a line break, a tab, accents, an emoji and HTML is loaded so you can see every format at work.
- Choose "Escape" to protect the text, or "Unescape" to read an escaped text.
- Pick the format. With "Detect automatically" the tool looks for signs such as
\n,%C3%A9or&and tells you which formats it could be; if several fit, it lists them. - Adjust the options that apply to the format: ASCII-only output, quote style, SQL dialect, CSV delimiter and always-quote, or shell quoting.
- Copy the result. The button "Use as input and unescape" (or "escape") swaps the result into the input so you can check the round trip.
Examples
JSON and JavaScript
The text He said "hi" followed by a line break and Bye becomes He said \"hi\"\nBye. With the ASCII option, é😀 becomes é😀: the emoji is written as a surrogate pair of two \u escapes, exactly as JSON requires.
HTML and XML
<b>it's</b> & more becomes <b>it's</b> & more in HTML. In XML the apostrophe is written ' instead of ', which is the one difference between them.
URL
a b&c=ç becomes a%20b%26c%3D%C3%A7. The ç is first converted to UTF-8 (two bytes) and each byte is percent-encoded. For a full URL with its parts use the URL encoder.
SQL, shell and regular expressions
In standard SQL O'Brien becomes O''Brien; in the MySQL dialect it is O\'Brien. In the shell, it's $HOME becomes 'it'\''s $HOME' with single quotes, which stops the shell from expanding $HOME. In a regular expression 1+1=2 (a.b) becomes 1\+1=2 \(a\.b\).
CSV and a bad escape
In CSV, a,"b" becomes "a,""b""": the field is wrapped in quotes and each quote is doubled. When you unescape the JSON text bad \q, the tool reports line 1, column 5: \q is not a valid escape. For 100% in URL mode it reports that a % must be followed by two hexadecimal digits, and that a literal percent is written %25.
Common mistakes
- Escaping twice. Escaping text that is already escaped turns
\ninto\\n. If a result looks doubled, unescape once and compare. - Using the wrong format. Percent-encoding is not HTML escaping, and JSON is stricter than JavaScript (no raw line breaks, no
\x41or\'). Pick the format of the place where the text will land. - Building SQL by hand. Escaping quotes is not a defence against SQL injection; use parameterized queries. Escape only for display, logs or one-off scripts.
- Forgetting the dialect. MySQL accepts
\', but standard SQL (PostgreSQL, SQLite, SQL Server) does not: use the doubled quote. - Raw line breaks in JSON strings. A JSON string cannot contain a real line break or tab; write
\nand\t. - A lone backslash at the end. An escape needs a character after it; the tool warns when the text ends with
\.
Frequently asked questions
What is the difference between escaping and encoding?
Escaping protects special characters inside a format, keeping the rest of the text as it is. Encoding, such as Base64, transforms the whole content. For that, use the Base64 tool.
Does it handle emoji and accents?
Yes. Characters outside ASCII are kept as they are, or written as \uXXXX when you choose the ASCII option. Characters above U+FFFF are written as two surrogate escapes in JSON and JavaScript.
How do I escape HTML only for the five special characters?
Use the HTML format: it replaces &, <, >, " and '. For named entities and many more symbols, see the HTML entities tool.
Can I unescape JSON I got from an API?
Yes. Choose Unescape and JSON, paste the contents between the quotes, and the tool returns the original text, or the line and column of the first invalid escape. To format the whole JSON document use JSON prettify.
Is my text sent to a server?
No. All the processing happens in your browser.
String escape in code
JavaScript
console.log(JSON.stringify('He said "hi"\nBye')); // "He said \"hi\"\nBye"
console.log(encodeURIComponent('a b&c=ç')); // a%20b%26c%3D%C3%A7
console.log('1+1=2 (a.b)'.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); // 1\+1=2 \(a\.b\)Python
import json, html, urllib.parse, shlex
print(json.dumps('He said "hi"\nBye')) # "He said \"hi\"\nBye"
print(html.escape("<b>it's</b>")) # <b>it's</b>
print(urllib.parse.quote("a b&c=ç")) # a%20b%26c%3D%C3%A7
print(shlex.quote("it's $HOME")) # 'it'"'"'s $HOME'Bash
printf '%q\n' "it's \$HOME" # it\'s\ \$HOME