Skip to main content
ponderloop

String Escape / Unescape

Escaped as a JSON string, the example text becomes She said \"hello\" to the café,\nthen opened C:\\temp\\new.txt. Escaped 5 of 57 characters; the result is 62 characters long. Pick the target format first, because the same text needs different escapes in JavaScript, Python, SQL, CSV, a shell or a regex, then choose Escape or Unescape: unescaping reports an invalid sequence with its position instead of guessing.

Updated Runs in your browser: nothing you enter is uploadedHow this tool works

Converted on this device

Nothing you paste is uploaded or kept: the text is converted by a script on this page and is gone when you reload.

Mode
Options

RFC 8259: a double-quoted string. Quotes, backslashes and the control characters U+0000 to U+001F must be escaped; everything else may stay as it is.

Result

Escaped 5 of 57 characters; the result is 62 characters long.

Sequences: 2 × \", 2 × \\, 1 × \n

Escape table for this format

What the converter writes for each character, and how often it applied to the text above.

Character nameCharacterWritten asIn this text
Double quote"\"2
Backslash\\\2
BackspaceU+0008\b0
Form feedU+000C\f0
Newline (LF)U+000A\n1
Carriage return (CR)U+000D\r0
TabU+0009\t0
Other control charactersU+0000 to U+001F\u0000 to \u001f0
A lone surrogate (half of a pair)U+D800 to U+DFFF\ud83d0

Common escape sequences by language

Each cell is the converter’s own output for the one character in the first column; (unchanged) means it is written as it is. JavaScript uses double quotes and Python single quotes here.
CharacterJSONJavaScript (double quotes)Java / CPython (single quotes)RegexXML attribute
Newline (LF)\n\n\n\n\n

Tab\t\t\t\t\t	
Carriage return (CR)\r\r\r\r\r
Backspace\b\b\b\x08\x08(unchanged)
Form feed\f\f\f\x0c\f(unchanged)
Vertical tab\u000b\v\013\x0b\v(unchanged)
NUL (U+0000)\u0000\0\000\x00\x00(unchanged)
Backslash\\\\\\\\\\(unchanged)
Double quote\"\"\"(unchanged)(unchanged)"
Single quote(unchanged)(unchanged)(unchanged)\'(unchanged)'
é (U+00E9) with the Unicode option on\u00e9\u00e9\u00e9\xe9(unchanged)(unchanged)
€ (U+20AC) with the Unicode option on\u20ac\u20ac\u20ac\u20ac(unchanged)(unchanged)
😀 (U+1F600) with the Unicode option on\ud83d\ude00\ud83d\ude00\ud83d\ude00\U0001f600(unchanged)(unchanged)

Quote handling compared across formats

Five probe strings written with the wrapping quotes switched on, so the delimiters show. ↵ stands for a real line break in the output.
Format and quote stylea"b becomesa'b becomesa`b becomesa\b becomesa, line break, b becomes
JSON string"a\"b""a'b""a`b""a\\b""a\nb"
JavaScript, double quotes"a\"b""a'b""a`b""a\\b""a\nb"
JavaScript, single quotes'a"b''a\'b''a`b''a\\b''a\nb'
JavaScript, template literal`a"b``a'b``a\`b``a\\b``a\nb`
Java / C string"a\"b""a'b""a`b""a\\b""a\nb"
Python, single quotes'a"b''a\'b''a`b''a\\b''a\nb'
Python, double quotes"a\"b""a'b""a`b""a\\b""a\nb"
CSV field"a""b""a'b""a`b""a\b""a↵b"
SQL string literal'a"b''a''b''a`b''a\b''a↵b'
Shell, single quotes'a"b''a'\''b''a`b''a\b''a↵b'
XML / HTML attribute"a"b""a'b""a`b""a\b""a
b"

String escape examples

Each row is the converter’s own result for the input in the first column, with the options named in the second.
InputFormat and optionsEscaped result
She said "hi"JSON string (No quotes added)She said \"hi\"
C:\temp\newJavaScript string (Double quotes)C:\\temp\\new
it'sSQL string literal (No quotes added)it''s
price (USD) $5.00 [new]Regular expression (Literal match)price \(USD\) \$5\.00 \[new\]
it'sShell single quotes (Always wrapped)'it'\''s'
a,b "c"CSV field (RFC 4180) (Quoted because it must be)"a,b ""c"""
<a href="x?a=1&b=2">XML / HTML attribute (Safe in either attribute quote)&lt;a href=&quot;x?a=1&amp;b=2&quot;&gt;
café 😀JSON string (Unicode option on)caf\u00e9 \ud83d\ude00
café 😀Python string (Unicode option on)caf\xe9 \U0001f600

Which escape syntaxes each language reads when unescaping

Each cell is what unescaping the sequence in the first column gives: the decoded character, error, or a result marked * when the tool added a warning (Python keeps an unknown escape as written).
SequenceJSONJavaScriptJava / CPython
\x41errorAAA
\u0041AAAA
\u{41}errorAerrorerror
\U00000041errorU00000041AA
\101errorerrorAA
\0errorNULNULNUL
\///error\/ *
\'error'''
\aerroraBELBEL
\verrorVTVTVT
\qerrorqerror\q *

Error messages when unescaping goes wrong

The first message the tool gives for each input. Positions count characters (not UTF-16 units) from 1, with the line and column in the pasted text.
FormatInputMessage
JSON stringab\u12G4Position 3 (line 1, column 3): \u needs 4 hex digits, found "12G4".
JSON stringabc\Position 4 (line 1, column 4): A backslash at the very end has nothing to escape (unterminated escape).
JSON stringa\qbPosition 2 (line 1, column 2): \q is not a JSON escape. JSON has \" \\ \/ \b \f \n \r \t and \uXXXX.
JSON stringsay "hi"Position 5 (line 1, column 5): A " inside a JSON string must be written \" (if it was meant to close the string, the opening quote is missing).
JSON stringline1↵line2Position 6 (line 1, column 6): A raw U+000A character is not allowed in a JSON string; write it as \n.
JavaScript string\u{110000}Position 1 (line 1, column 1): \u{110000} is above U+10FFFF, the last Unicode code point.
Python string\N{DEGREE SIGN}Position 1 (line 1, column 1): \N{name} escapes need the Unicode name table, which this tool does not carry; write \uXXXX or \UXXXXXXXX instead.
CSV field (RFC 4180)"abcPosition 1 (line 1, column 1): This quoted field is never closed: the closing double quote is missing.
SQL string literal'O'Brien'Position 3 (line 1, column 3): A single ' inside a SQL string literal must be doubled ('') or it ends the literal.
XML / HTML attributea &nbsp; bPosition 3 (line 1, column 3): &nbsp; is not one of the five XML entities (amp, lt, gt, quot, apos) or a numeric reference. For HTML names such as &nbsp; use the HTML entity decoder.
Shell single quotes'abcPosition 1 (line 1, column 1): This single quote is never closed: the closing quote is missing.
Regular expression\d+Position 1 (line 1, column 1): \d is a regex token (a character class, anchor, group reference or flag), not an escaped literal character, so it has no plain-text form.

How to escape or unescape a string

  1. Choose the format. Select the language or syntax the string is going into: JSON string, JavaScript string, Java / C string, Python string, CSV field (RFC 4180), SQL string literal, Regular expression, Shell single quotes, XML / HTML attribute. Each has its own escape table, shown under the result.
  2. Choose Escape or Unescape. Escape turns plain text into something safe to put in that format. Unescape reads a string that already has escapes and gives back the plain text; the surrounding quotes are optional.
  3. Paste the text. Paste or type in the box. The example, She said "hello" to the café,↵then opened C:\temp\new.txt, is already there; replacing it updates the result as you type.
  4. Set the options. For JavaScript and Python pick the quote style. Tick the Unicode option to write every character above U+007E as an escape, and the quotes option to add or remove the surrounding quotes.
  5. Copy the result or swap. Press Copy to copy the result. Swap moves the result into the input and flips the mode, so you can check that unescaping gives the original text back.

Frequently asked questions

How do I escape a string for JSON?
Put a backslash before each double quote and backslash, and write line breaks and tabs as \n and \t; every other control character from U+0000 to U+001F becomes \u followed by four hex digits. Everything else, including accented letters and emoji, may stay as it is. The example text becomes She said \"hello\" to the café,\nthen opened C:\\temp\\new.txt. Escaped 5 of 57 characters; the result is 62 characters long. Add the quotes with the quotes option: x becomes "x".
How do I escape quotes in a string?
It depends on the language, so choose the format first. A double quote on its own becomes: JSON string: \"; JavaScript string: \"; Java / C string: \"; Python string: "; SQL string literal: "; Shell single quotes: '"'; CSV field (RFC 4180): """"; XML / HTML attribute: &quot;. In SQL the single quote is the one to double, O''Brien for O'Brien, and in a shell single-quoted string a quote needs the close-escape-reopen form, 'it'\''s' for it's. The quote comparison table lists all five probe strings side by side.
What is the difference between escaping and unescaping?
Escaping rewrites characters that would otherwise end the string or be misread, such as a quote, a backslash or a line break, as an escape sequence. Unescaping reverses it. The two are exact inverses for the same format and options: escaping She said "hello" to the café,↵then opened C:\temp\new.txt as a JSON string and unescaping the result returns the original, which the Swap button lets you check in one click. Unescaping text that was never escaped usually changes nothing, and invalid escapes are reported with a position rather than skipped.
What is the difference between JSON and JavaScript string escaping?
JSON is a strict subset. JSON has only \" \\ \/ \b \f \n \r \t and \uXXXX, and raw control characters are not allowed inside a string. JavaScript also has \v, \0, \xHH, \u{...}, \' and a line continuation, and it lets you choose single quotes, double quotes or a template literal. Unescaping it\'s as JSON fails at position 3 (line 1, column 3) because \' is not a JSON escape, while a JavaScript single-quoted string accepts it. The line separator character U+2028 stays as it is in JSON but is written \u2028 in JavaScript, because older JavaScript engines treat it as the end of a line.
How do I escape a string in JavaScript with single quotes, double quotes or backticks?
Escape the quote you used to open the string and leave the others alone. With single quotes it's becomes it\'s; with double quotes the apostrophe stays as it is. In a template literal the backtick and the start of an interpolation need escaping: ${name} becomes \${name}. A NUL character is written \0, but \x00 when a digit follows, since \01 would be read as an octal escape: \x001 for NUL then 1.
How does the tool escape non-ASCII characters and emoji?
By default it leaves them alone, because JSON, JavaScript, Java and Python all accept Unicode text. With the Unicode option on, every character above U+007E is written as an escape. A character above U+FFFF is a surrogate pair in JSON, JavaScript and Java: é followed by 😀 becomes \u00e9\ud83d\ude00 in JSON and \u00e9\ud83d\ude00 in Java. Python does not combine surrogate escapes, so the same text becomes \xe9\U0001f600, with a \xHH, \uXXXX or \UXXXXXXXX form like ascii(). A lone surrogate is not text at all, so it is always written as an escape.
Is escaping quotes enough to prevent SQL injection?
No. Doubling single quotes (O''Brien) builds a correct literal for standard SQL, but it is not a defence you should rely on: dialects differ (MySQL also treats the backslash as an escape unless NO_BACKSLASH_ESCAPES is set), character-set tricks exist, and numbers and identifiers are not strings at all. Use parameterised queries, also called prepared statements, which send the value separately from the statement, as the OWASP SQL injection prevention cheat sheet recommends. Use this tool only to write a literal by hand, for example in a one-off script.
How do I escape a CSV field?
RFC 4180 has one rule: a field that contains a comma, a double quote or a line break is wrapped in double quotes, and each double quote inside is written twice. say "hi", then leave becomes "say ""hi"", then leave", and unescaping that returns say "hi", then leave. There is no backslash escape in CSV. A field with none of those characters is left bare; the quotes option wraps it anyway, which some importers want.
How do I escape a regular expression?
Escape the characters that have a meaning of their own: \ ^ $ . | ? * + ( ) [ ] { } and the slash of a regex literal. 1+1=2 (a|b)? becomes 1\+1=2 \(a\|b\)\?, which matches that exact text. The hyphen is left alone, so a-b stays a-b: in JavaScript with the u flag an escaped hyphen outside a character class is a syntax error, and inside a class you must place it yourself. Unescaping removes the backslash from a literal, and refuses sequences like \d that are tokens, not escaped characters.
Why does unescaping show an error instead of a result?
Because a half-decoded string is worse than none. The tool reads every escape by the rules of the chosen format and stops with the position of each problem: a bad \u sequence, a backslash at the very end, an unknown escape, a raw quote or an unterminated quote. For ab\u12G4 as JSON the message is: Position 3 (line 1, column 3): \u needs 4 hex digits, found "12G4". Fix the text at that position, or check you picked the right format; the error table above shows the message for a dozen cases.
Why do I get double backslashes?
The text was escaped twice. Escaping a"b as JSON gives a\"b, and escaping that result again gives a\\\"b, because each backslash is now text that needs its own escape. The same thing happens with a literal backslash-n typed by hand: \\n in JSON, because JSON must tell it apart from a real line break. Unescape once for each time it was escaped.
What does a Python raw string do, and can the tool unescape one?
In a raw string, written with an r before the quote, backslashes are not escapes, so there is nothing to decode: r"C:\temp\new" holds exactly C:\temp\new. The tool recognises the prefix, shows the text between the quotes as it is and says so. A raw string cannot end in a single backslash, and a quote inside it keeps its backslash, which is why escaping for Python writes a normal literal rather than a raw one.
Which characters become octal escapes in Java and C?
The control characters without a short form, U+0001 for one, become a three-digit octal escape, \001. Octal is used rather than \u because a Java compiler translates a \uXXXX escape before it reads the string, so \u000a would end the line inside the literal. The short forms \b \t \n \f \r, the double quote and the backslash are written as such. Decoding also accepts the C-only \a, \v, \? and \xHH.
Does this tool upload what I paste?
No request is made with your text. Escaping and unescaping are plain string functions running on this page, read by hand with no eval and no dynamic code, so even a hostile pasted string is only ever treated as data. Reloading clears the box.

How it works

Each format has an escape table in the code (the named escapes of that format, in order) and the converter and the table on the page both read it, so the table cannot describe an escape the converter does not make. Text is processed by code point; JSON, JavaScript, Java and Python write a character above U+FFFF as a pair of \u escapes (Python as \xHH, \uXXXX or \UXXXXXXXX, which is what its ascii() does), and a lone surrogate is always escaped. Escaping rules: JSON follows RFC 8259 (quote, backslash and U+0000 to U+001F; the Unicode option writes everything above U+007E, DEL included, as json.dumps does with ensure_ascii); JavaScript escapes the chosen quote, the backslash, the control characters and U+2028/U+2029, plus the dollar sign before a brace in a template literal; Java and C write controls as three-digit octal and never as \u; Python writes controls and DEL as \xHH like repr(); CSV follows RFC 4180 (quote by need, double the quotes); SQL doubles the single quote; the regex table escapes the syntax characters of the JavaScript RegExp grammar plus the slash; the shell form wraps in single quotes and writes a quote as '\''; XML writes five references and numeric references for tab and line breaks. Unescaping is a hand-written scanner per format, with no eval and no dynamic code: it never runs the text. Text that is valid as written is read as written, otherwise the surrounding quotes are removed and the inside read, so escaping then unescaping is exact even for a text that itself starts and ends with a quote (the quotes checkbox puts the wrapped reading first). Every problem is reported with its position as a character number, line and column, up to eight at a time, and a result with problems is never shown. The independent checks behind the tests: the JSON, Python and CSV output equals Python's json.dumps, repr, ascii and csv.writer for a fixed corpus, and the JSON, JavaScript, Python, Java, C, SQL, shell and XML output was read back through the real consumer of each format (a JavaScript engine, Python, javac, gcc, sqlite3, bash and an XML parser). Escaping is a text transformation, not a security control: do not use it in place of parameterised queries or an HTML sanitiser.

Sources

More: Encoding and number systemsAll developer toolsAll toolsHome & DIY calculators