Escape and Unescape Characters
Turn quotes, newlines, tabs and backslashes into \n-style escape sequences for string literals, or decode escaped log and JSON text back to plain text.
The interactive tool loads here when JavaScript runs.
About this toolWhat is the Escape Characters tool?
The Escape Characters tool converts between plain text and backslash-escaped text, the form used inside string literals in JavaScript, JSON, Java, C, C#, Python and many other languages. Add Escapes turns a real line break into \n, a tab into \t, a double quote into \", a single quote into \' and a backslash into \\. Remove Escapes does the reverse.
The full set handled is \n, \t, \r, \", \', \\, \b (backspace), \f (form feed), \v (vertical tab) and \0 (null). Both directions work in a single pass, so an escaped backslash followed by a letter, as in C:\\new, decodes to C:\new and not to "C:" plus a line break. Escaping and then unescaping always gives back the original text.
Unicode escapes such as \u00e9 and hex escapes such as \x41 are left as they are, and so are unknown sequences. HTML entities, URL percent-encoding and SQL quote doubling are different schemes; see the tips. Processing runs in your browser.
- Escape and unescape in one tool
- Handles \n, \t, \r, quotes, backslash, \b, \f, \v and \0
- Single-pass conversion that round-trips exactly
- Built-in reference of escape sequences
- Runs in your browser; nothing is uploaded
Updated
How to use the Escape Characters tool
- Paste text into Input Text.
- Click Add Escapes to turn line breaks, tabs, quotes and backslashes into escape sequences.
- Or click Remove Escapes to turn sequences such as \n and \" back into real characters.
- Check Output Text and click Copy Result. Use the Escape Characters Reference below the tool for the meaning of each sequence.
Worked examples
Preparing text for a string literal
Input
He said "hi" C:\temp
Output
He said \"hi\"\nC:\\temp
The quotes gain backslashes, the line break becomes \n and the backslash in the path is doubled. The output is one line and can sit between double quotes in JavaScript, Java or C#.
Reading an escaped log message
Input
Error at line 3:\n\tunexpected \"}\"
Output
Error at line 3: unexpected "}"
Remove Escapes turns the \n into a real line break, \t into a tab and \" into a plain quote, so a message copied from a JSON log becomes readable.
Background and insight
Backslash escapes date back to C in the early 1970s, and the C set (\n, \t, \r, \b, \f, \v, \0, \\, \', \") was copied almost unchanged into Java, JavaScript, C#, Python and PHP. That shared history is why one tool can serve all of them for everyday text. The differences are at the edges: JSON (RFC 8259) allows \b, \f, \n, \r, \t, \", \\, \/ and \uXXXX but not \' or \v or \0, and Python treats a backslash before an unknown letter as a literal backslash while JavaScript silently drops it.
The classic bug is double escaping. Text passes through several layers (a database, a JSON API, a log line, a JavaScript string) and each layer adds its own backslashes, so a Windows path ends up as C:\\\\Users. Remove Escapes undoes one layer per click, so clicking twice is a quick way to see how many layers you are dealing with.
Escaping for a string literal is not the same as escaping for safety. Backslash escapes do nothing to stop SQL injection or cross-site scripting. Use parameterised queries for SQL and context-aware output encoding for HTML; those are the only reliable defences.
Common use cases
Writing string literals
Escape multi-line text, quotes and paths before pasting them into source code or a test fixture.
Reading JSON and log output
Unescape messages and stack traces copied from JSON logs, where every line break appears as \n.
Debugging double escaping
Remove one layer of escapes at a time to find where an extra layer of backslashes was added.
Building config values
Escape newlines in a certificate or a multi-line message that has to live in a single-line environment variable.
Tips and limitations
- Add Escapes also escapes single quotes. JSON does not allow \'; for strict JSON, use JSON.stringify or the JSON formatter instead.
- HTML needs entities (<, &, "), not backslashes, and URLs need percent-encoding. The Encoding Tools page handles both.
- SQL string literals escape a single quote by doubling it (''), not with a backslash, in standard SQL, PostgreSQL and SQL Server. Prefer query parameters anyway.
- Unicode escapes like \u00e9 are left unchanged by Remove Escapes. Decode those with JSON.parse or a language REPL.
- Windows line endings become \r\n when escaped, because the carriage return is escaped separately.
Sources and further reading
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format, IETF. Section 7 lists the escapes JSON allows
- Lexical analysis: escape sequences, Python Software Foundation. Python's escape table and its handling of unknown escapes
- String: escape sequences, MDN Web Docs
- SQL Injection Prevention Cheat Sheet, OWASP. Parameterised queries, not escaping, as the defence
- Cross Site Scripting Prevention Cheat Sheet, OWASP. Context-aware output encoding for HTML
Frequently asked questions
How do I convert \n into real line breaks?
Paste the text and click Remove Escapes. Every \n becomes a real line break, \t becomes a tab, and \" becomes a quote. An escaped backslash (\\) becomes one backslash, and the tool reads it before the following letter, so \\n stays as a backslash followed by n.
What is the difference between escaping for JSON and for JavaScript?
JSON allows fewer escapes. Both accept \n, \t, \r, \b, \f, \", \\ and \uXXXX, but JSON rejects \' and \v, which JavaScript accepts. Because Add Escapes also escapes single quotes, its output is valid in JavaScript strings; for strict JSON, generate the value with JSON.stringify.
Why does my string have double backslashes everywhere?
It has been escaped twice, usually because it passed through two layers that each added escapes, such as a JSON API returning a JSON-encoded string. Click Remove Escapes once per layer. If \\n turns into \n after one click and a line break after two, there were two layers.
Does escaping characters protect against SQL injection?
No. Backslash escaping is about writing string literals, not about security, and many SQL dialects do not even treat the backslash as an escape. Use parameterised queries or prepared statements, which send the values separately from the SQL text so they can never be interpreted as code.
Can this tool escape HTML or URLs?
No. HTML uses entities such as < and &, and URLs use percent-encoding such as %20. Those are different schemes from backslash escapes. Use the Encoding Tools page, which handles Base64, URL and HTML encoding, for those.
What does \0 mean in an escaped string?
It is the null character, code point 0. C uses it to mark the end of a string, which is why a stray null in data can truncate text in C-based programs. JavaScript and Python treat it as an ordinary character. Add Escapes writes it as \0 so it becomes visible.
Is there an in-depth guide to the Escape Characters tool?
Yes. “Escape Sequences Explained: Backslashes in JSON, JavaScript, Python and SQL” covers the background, how it works, common pitfalls and worked examples. Read the full guide.
Related Tools You May Like
Free, in-browser tools that pair well with this one.