String Escape & Unescape
Escape text for a JSON string, JavaScript literal, SQL literal, HTML, shell argument, CSV field or regex, and unescape it back; rules from the specifications.
The exact characters to paste into code, a query, a template or a shell — with the rule that produced them named — and the original text recovered from an escaped one.
Example: say "hi" with a newline → "say \"hi\"\n" for JSON; O'Neil → 'O''Neil' for SQL; 1+1=2? → /1\+1=2\?/ for a regex; <a> → <a> for HTML.
Seven contexts,
seven different rules.
Which characters each context reserves, how they are written, what unescaping accepts, and why escaping is not a security boundary.
The rules
JSON (RFC 8259): quote and backslash get a backslash; newline, return, tab, backspace and form feed become \n \r \t \b \f; other control characters become \uXXXX. JavaScript: the same plus single quotes, backticks and ${for template literals. SQL: a single quote is doubled ('') — the standard's only escape inside a literal. HTML: & < > " and ' become character references, which is safe for both text nodes and double-quoted attributes. Shell: the POSIX single-quoted word, where a quote inside is written as '\'' (close, escaped quote, reopen). CSV (RFC 4180): a field containing a quote, comma or line break is wrapped in quotes with inner quotes doubled; otherwise it is left alone. Regex: the ECMAScript metacharacters get a backslash so the text matches itself.
Unescaping
The reverse applies each rule backwards and strips the context's surrounding quotes if present. JSON is parsed strictly, so an invalid escape such as \q is refused with the parser's reason. JavaScript accepts \n-style, \xHH, \uHHHH and \u{…} forms. HTML resolves numeric references and the five named ones this page writes plus ; other named entities are left as typed.
What escaping is not
Escaping makes a literal out of text you control; it does not make untrusted input safe. SQL should use parameterised queries, HTML should use a templating engine that escapes by context, and shells should receive arguments as arrays. Windows cmd.exe quoting, URL percent-encoding (see URL Encode & Decode) and Base64 are different operations. Nothing you enter leaves the browser; the same four anonymous usage counts as the rest of the site apply.
SOURCES
- RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format, §7 Strings
- ECMAScript Language Specification — string literals and RegExp syntax
- RFC 4180 — Common Format and MIME Type for CSV Files
- WHATWG HTML Living Standard — character references
- POSIX.1-2024 Shell Command Language — §2.2.2 Single-Quotes
Last reviewed 19 September 2026. How results are checked: How we verify.