Unescape String: Turn Escape Sequences Back into Real Characters

Learn what it means to unescape a string, how to identify the escaping format, how to convert sequences like \n and \" back to real characters, and how to verify the result.

What unescaping a string does

Escaping is a way of writing characters that cannot appear directly inside a string literal or a data format. Usually this means a backslash followed by a code. For example, \n stands for a line break, \t stands for a tab, and " stands for a double quote that would otherwise end the string. To unescape a string is to reverse this. The operation reads each escape sequence and replaces it with the single character it represents.

The input is text that contains escape sequences. Typical sources are values copied from source code, JSON data, log files or API responses. The output is the same text with those sequences converted into actual characters. Ordinary letters, digits and punctuation that are not part of an escape sequence should pass through unchanged. In the illustrative example below, [TAB] marks an invisible tab character.

Escaped:    Name:\t\"Zoë\"\nPath: C:\\temp

Unescaped:  Name:[TAB]"Zoë"
            Path: C:\temp

In this example, \t becomes a tab and " becomes a plain quote. The \n becomes a real line break, so the result spans two lines. The doubled backslash \ collapses into one literal backslash.

Common escape sequences

Sequence | Unescaped result
\n | Line feed (new line)
\r | Carriage return
\t | Horizontal tab
" | Double quote
' | Single quote (in formats that allow it)
\ | One literal backslash
\uXXXX | The Unicode character with that hexadecimal code, e.g. \u00e9 becomes é

In JSON and JavaScript, \uXXXX holds one UTF-16 code unit. Characters outside the Basic Multilingual Plane therefore need two sequences, called a surrogate pair. Most emoji fall into this group. For example, \ud83d\ude00 is a single emoji. A correct unescaper combines the two halves into one character rather than producing two broken symbols.

Identify the escaping format first

There is no single universal set of escape rules. JSON, programming-language string literals (C, JavaScript, Python and others), regular expressions, HTML entities and URL percent-encoding each have their own syntax. Here are some contrasts. \x41 is valid in Python and JavaScript strings but not in JSON. HTML uses forms like & and < instead of backslashes. URLs use %20 for a space. A tool built for one format may reject input in another format, or worse, convert it into the wrong characters.

  • Backslash sequences with \uXXXX, inside double quotes: likely JSON or a JavaScript-style string.
  • Backslash sequences including \x or octal forms such as \101: likely a C-family or Python string literal.
  • Ampersand codes ending in a semicolon, such as " or ': HTML or XML entities, which need an HTML decoder instead.
  • Percent signs followed by two hex digits, such as %2F: URL encoding, which needs a URL decoder instead.

Step by step: unescape a string

  • 1. Copy the escaped text exactly, without trimming spaces or retyping characters. Include the surrounding quotes only if your tool expects a complete string literal.
  • 2. Decide which format the text uses (see the section above) and choose a tool or function for that format.
  • 3. Unescape the text once. Do not run it through the tool a second time to be sure.
  • 4. Inspect the result with a view that makes invisible characters visible, such as an editor option that shows tabs, spaces and line endings.
  • 5. Verify the result using the checks described below before you use it anywhere important.

Doing it in code (illustrative examples)

For JSON-style escapes, a common approach is to wrap the escaped text in double quotes and pass it to a standard JSON parser. The snippets below show this pattern. They are examples only and their output has not been tested here.

// JavaScript
const escaped = 'Line one\\nLine two \\u00e9';
const text = JSON.parse('"' + escaped + '"');

# Python
import json
escaped = r'Line one\nLine two \u00e9'
text = json.loads('"' + escaped + '"')

This trick only works if the input is valid JSON string content. If the text contains an unescaped double quote, a raw line break or an escape that JSON does not support, such as \x41, the parser raises an error instead of returning a result. In Python, decoding with the unicode_escape codec is sometimes suggested as an alternative. It can garble non-ASCII characters that were already present in the text, such as é or Cyrillic letters, so check its output carefully.

Pitfalls that change meaningful content

  • Double unescaping: the text \n should unescape to a literal backslash followed by n. A second pass turns that into a line break, which changes the content.
  • Unescaping text that was never escaped: a Windows path such as C:\new\table contains \n and \t. An unescaper will turn these into a line break and a tab, destroying the path.
  • Using the wrong format: HTML entities or percent-encoded text will not be decoded by a backslash unescaper. Backslash rules from one language may also misread sequences from another.
  • Invalid or unknown sequences: tools differ here. Some report an error, some leave the sequence unchanged and some silently drop the backslash. Check how your tool behaves before trusting its output.
  • Line endings: \r\n produces a Windows-style line ending of two characters. \n alone produces a single line feed. Mixing them can affect diffs, parsers and checksums.

How to verify the result

  • Spot-check known sequences: confirm that each \n became a real line break, each \t became a tab and each " became a plain quote, with invisible characters shown.
  • Compare lengths: each two-character sequence such as \n becomes one character, and each six-character \uXXXX becomes one character. The unescaped string should therefore be shorter by a predictable amount.
  • Round-trip test: escape the result again in the same format and compare it with the original. Small differences can be legitimate, because é and \u00e9 are equivalent in JSON, as are / and /. Any other difference points to a problem.
  • Test a small sample first: run a short string whose correct result you already know through the same tool before you process the full text.

Limits of this guide

No specific unescape tool was evaluated for this guide. Statements about how a particular tool handles invalid sequences, surrogate pairs, octal or \x escapes, or line endings are therefore not confirmed here. Check your chosen tool's documentation, or test it with a known sample, before relying on it for important data.