String escape and unescape
Escape special characters so text is safe inside JSON, JavaScript, HTML, a URL, a SQL string or a regex, or reverse it. Choose the direction and the format. Free, and your text stays in your browser.
How to use it
- Paste your text into Text.
- Choose Escape or Unescape in Direction.
- Pick the target in Format: JSON, JavaScript, HTML, URL, SQL or regex.
- Copy the Result.
Complete guide to escaping and unescaping text
What escaping is and why it matters
Many languages give a few characters a special job. A double quote ends a string, a backslash starts an escape sequence, an angle bracket opens an HTML tag, a percent sign introduces a code in a URL. When your text itself contains one of those characters, you must "escape" it, which means writing it in a form that is read as plain text. Forget to do it and you get broken JSON, a string literal that does not compile, an HTML page that swallows part of your text, or in the worst case an injection hole. This tool escapes text for six targets, and reverses the process when you need to read an escaped value.
The six formats
| Format | What Escape does | Use it for |
|---|---|---|
| JSON string | Backslash before quotes and backslashes; \n, \t and similar for control characters | Putting text inside a JSON value |
| JavaScript string | Same idea, plus single quotes, backticks and ${ | Code in single, double or template quotes |
| HTML entities | &, <, >, " and ' become &, <, >, ", ' | Showing code or user text inside a page |
| URL (percent-encoding) | Every character unsafe in a URL component becomes a % code, in UTF-8 | Query values and path segments |
| SQL string (simple) | A single quote becomes two single quotes | Quick manual queries |
| Regular expression | A backslash before characters with special meaning | Searching for literal text in a pattern |
Choose Unescape in Direction to go the other way. JSON and JavaScript unescape read sequences such as \n, \t, \xE9 and \u00e9. HTML unescape converts named entities like &, and € plus any numeric entity. An unknown name is left untouched.
Worked examples
- Type two lines into Text:
She said "hello"andPath: C:\temp\file. With Format on JSON string and Direction on Escape, the Result isShe said \"hello\"\nPath: C:\\temp\\file. The line break became\n, each backslash was doubled, and there are no surrounding quotes, so you add your own. - Escape
<a href="x">Tom & Jerry's</a>as HTML entities and you get<a href="x">Tom & Jerry's</a>, which a browser displays as the original text. - Escape
O'Brienas SQL string (simple) and you getO''Brien. - Escape
price (USD) = 5.99?as Regular expression and you getprice \(USD\) = 5\.99\?. - Escape
a b&c/ças URL (percent-encoding) and you geta%20b%26c%2F%C3%A7. - Paste
Line1\nLine2 \u00e9, choose Unescape and JSON string, and the result is the two linesLine1andLine2 é.
Tips and common mistakes
- Do not escape twice. Escaping already-escaped text doubles every backslash and produces text that no longer means what you intended.
- Escape for the context where the text will be used, not the one where you typed it. A value for a URL needs percent-encoding even if the rest of your document is JSON.
- Escaping makes text safe to embed, but it does not replace parameterized queries for databases or output encoding frameworks in real applications. The SQL option is meant for quick manual work.
- When unescaping, an invalid sequence such as a lone backslash before
uwithout four digits, or a stray%, shows an error that tells you to check the text. - When decoding a URL, a plus sign stays a plus sign. It is not converted to a space.
Related tools
For more control over web addresses use the URL encoder and decoder and the URL parser. For HTML-only work see the HTML entities encoder, and to test a pattern after escaping it use the regex tester.
Questions
Does the JSON result include the surrounding quotes?
No. You get only the escaped content, so you can put it between your own quotes. For example He said "hi" becomes He said \"hi\".
What does the SQL option do?
It doubles single quotes (O'Brien becomes O''Brien), which is the standard SQL way. Some databases also treat backslashes specially, so prefer parameterized queries in real code instead of building SQL by hand.
How is URL encoding different from the other options?
It uses percent codes, like space becoming %20, and encodes every character that is not safe inside a URL component. When decoding, a + is kept as a plus sign.
Which characters does the regex option escape?
All characters with special meaning in a regular expression: . * + ? ^ $ { } ( ) | [ ] \ / and the hyphen. That makes a literal piece of text safe to use inside a pattern.