String Escape / Unescape
Escape or unescape text for JSON, JavaScript, Python, SQL, CSV, shell and regex.
About String Escape / Unescape
Escape or unescape a string for JSON, JavaScript, Python, SQL, CSV, the shell or a regular expression, with quotes, backslashes and Unicode handled exactly.
How to use
- Pick the format: JSON, JavaScript, Python, SQL, CSV, Shell or Regex.
- Choose Escape or Unescape. Regex is escape only.
- Paste the text. The result updates as you type, and an escape that is not valid is reported with its position.
- When escaping, Add quotes wraps the result in the format's quotes (JSON, JavaScript, Python, SQL) and ASCII only writes every non-ASCII character as an escape (JSON, JavaScript, Python).
What it does
- JSON: escapes
",\and control characters, giving the same text asJSON.stringify. With ASCII only, a character outside the Basic Multilingual Plane, such as an emoji, becomes a surrogate pair. - JavaScript: escapes both kinds of quote, backslashes, line breaks, and invisible characters as
\xHHor\uHHHH. The result is for a single-quoted or double-quoted string; backticks and${are not escaped, so it is not safe inside a template literal. - Python: the same for a
strliteral, with\UHHHHHHHHfor characters above U+FFFF when ASCII only is on. It does not produce bytes or raw-string literals. - SQL: doubles each single quote, the standard SQL rule used by PostgreSQL, SQLite, SQL Server and Oracle. Backslashes are left alone, so it is not enough for MySQL in its default mode, where a backslash is also an escape. It is no substitute for parameterised queries.
- CSV: treats the input as one field and quotes it when it holds a comma, a quote, a line break or a space at either end, doubling the quotes inside. It does not split text into rows or columns.
- Shell: wraps the text in single quotes and writes each single quote as
'\'', which sh, bash and zsh pass on unchanged. It is not for cmd or PowerShell. Unescape reads one shell word and expands nothing. - Regex: escapes
\ ^ $ . * + ? ( ) [ ] { } |and/, and writes line breaks and tabs as\n,\rand\t. - Unescape accepts the string with or without its surrounding quotes. It does not guess:
\x41is an error in JSON, an octal escape is an error in JavaScript, and Python's\N{name}form is not supported.
Example
She said "hi"
C:\temp
→ JSON, with quotes
"She said \"hi\"\nC:\\temp"
it's $HOME
→ Shell
'it'\''s $HOME'
Privacy
Escaping and unescaping run in the browser. No request is made, and nothing is stored.
Frequently asked questions
- How do I escape a string for JSON?
- Put a backslash before every double quote and backslash, write line breaks and tabs as \n and \t, and write any other control character as \u followed by four hex digits. Nothing else needs escaping, including single quotes and non-ASCII characters.
- How do you escape a single quote in SQL?
- Double it: the name O'Brien is written 'O''Brien'. This is standard SQL and works in every major database. For values that come from users, use a parameterised query instead of building the string yourself.
- How do I escape a string for a shell command?
- Wrap it in single quotes, inside which the shell treats every character literally, and replace each single quote in the text with '\'' (close the quotes, an escaped quote, open them again).
- Which characters need escaping in a regular expression?
- Fourteen characters have a special meaning outside a character class: \ ^ $ . * + ? ( ) [ ] { } and |. A forward slash also needs a backslash inside a JavaScript regex literal, because it would end the literal.