Home / Encoders & Decoders

Backslash Escape and Unescape Tool

Runs locally in your browser. Your input is not uploaded.

Backslash escaping represents characters such as line breaks, tabs, quotes, and backslashes with visible sequences. It is useful when preparing a string for a code sample, inspecting serialized text, or turning copied escape sequences back into readable content.

This tool follows a focused JavaScript/JSON-style model, not the complete grammar of a particular programming language. It transforms text only: it does not surround the result with quotes, validate a whole string literal, or account for the syntax rules of a shell, regular expression, SQL dialect, or template language.

How to Use the Backslash Escape and Unescape Tool

Choose a direction, then adjust the controls that apply to escaping.

  1. Select Escape in the Mode control.
  2. Enter content in Raw text and review the live result in Escaped output.
  3. Leave Escape quotes enabled to escape both single and double quotes, or clear it to preserve them.
  4. Enable Escape non-ASCII as \u to represent non-ASCII characters with Unicode escapes.
  5. Select Copy to copy the current output.
  6. Select Unescape, enter sequences in Escaped text, and read the result in Unescaped text.
  7. Select Swap to move the output into the input and switch to the opposite mode.

Which Characters the Escape Mode Converts

Escape mode converts backslashes to \\ and uses short forms for common control characters: \n, \r, \t, \b, \f, \v, and \0. Other control characters from U+0001 through U+001F, plus U+007F, are written as two-digit \x escapes. Quotes become \" and \' only when Escape quotes is enabled.

Non-ASCII text remains readable by default. When Escape non-ASCII as \u is enabled, characters in the Basic Multilingual Plane use four-digit forms such as \u2713. Code points above U+FFFF use brace notation such as \u{1f600} rather than a pair of UTF-16 surrogate escapes.

line one
line two ✓  →  line one\nline two \u2713

The output is a fragment, not a complete JSON or JavaScript literal. If you need valid JSON, the surrounding double quotes and any structural syntax still matter; use the JSON formatter to validate the complete document.

Supported Unescape Sequences and Edge Cases

Unescape mode recognizes \n, \r, \t, \b, \f, \v, and \0, along with \x followed by two hex digits, \u followed by four hex digits, and brace-form \u{...} containing one to six hex digits. Brace-form values above U+10FFFF are rejected because they are beyond the Unicode code space.

An incomplete \x or \u sequence produces an error, as does a lone backslash at the end. A four-digit \u escape is converted as one UTF-16 code unit, so a conventional surrogate pair such as \uD83D\uDE00 becomes the corresponding pair in the resulting string.

Unknown escapes are handled permissively: the backslash is removed and the following character is kept. Thus \q becomes q, while \", \', \\, and \/ become the character after the slash. This mirrors the helper’s intended JavaScript-like behavior, but it means the tool is not a strict JSON escape validator and can hide a typo if you expected an error.

Backslash Escaping Is Context-Dependent

Different parsers assign different meanings to backslashes. JSON supports a smaller escape set than JavaScript source, JavaScript regular expressions have their own grammar, and shells may process quotes and backslashes before a command receives them. A sequence that is correct in one layer can be changed again by the next.

Double escaping often appears when data passes through multiple representations. A newline stored as two visible characters, backslash and n, may need \\n inside a JSON string so that parsing the JSON produces \n rather than an actual line break. Work one layer at a time and identify which parser consumes each escape.

Backslash escaping is also different from URL percent-encoding and HTML entity encoding. Use the URL encoder and decoder for URL components, or the HTML entity encoder and decoder for text placed into HTML. Substituting one scheme for another can produce invalid data or introduce an injection bug.

Privacy and Security Considerations

Escaping and unescaping happen locally in the browser; the tool does not upload the entered text. Local processing limits exposure during conversion, but copied results can still contain credentials, personal data, terminal control characters, or other sensitive content.

Escaping is not sanitization. A backslash-escaped value is not automatically safe for HTML, a URL, SQL, a shell command, a regular expression, or JavaScript execution. Safety depends on using the correct encoder or parameterization mechanism for the exact output context.

Treat unescaped text as untrusted data. It may reveal invisible line breaks, null characters, or control codes that alter logs and command-line displays. Inspect the result before passing it to another parser, and avoid evaluating or executing content merely because the escapes were decoded successfully.

Frequently Asked Questions

What is the difference between escaping and unescaping?

Escaping replaces selected characters with visible backslash sequences, such as a line break becoming \n. Unescaping interprets supported sequences and restores their character values. Neither operation encrypts, validates, or executes the text.

Does Escape quotes handle both quote styles?

Yes. When enabled, it changes double quotes to \" and single quotes to \'. When disabled, both quote styles remain unchanged, although backslashes and supported control characters are still escaped.

How does the tool escape emoji?

With Escape non-ASCII as \u disabled, emoji remain as Unicode characters. With it enabled, a code point above U+FFFF uses brace notation such as \u{1f600}. That notation is valid in modern JavaScript source but is not part of JSON’s escape grammar, which represents such characters with UTF-16 surrogate pairs.

Why did \q become q instead of causing an error?

The unescape helper treats an unknown escape by dropping the backslash and keeping the following character. Only specific malformed cases, including incomplete \x, incomplete \u, an out-of-range brace escape, or a trailing lone backslash, produce errors. Use a parser for the target format when strict validation is required.

Can I use this output directly in a shell or regular expression?

Not safely by assumption. Shells and regular-expression engines have separate escaping rules, and the receiving language may process the text more than once. Treat this output as a text transformation and apply the target environment’s documented quoting or parameterization rules afterward.

References: MDN: JavaScript lexical grammar and escape sequences · RFC 8259: JSON string grammar.