Skip to main content
The Backslash Escape/Unescape tool converts special characters to their backslash-escaped equivalents for use in programming languages like JavaScript, JSON, Python, and more. It’s essential when working with strings that contain newlines, quotes, or other control characters.

Use Cases

  • Preparing strings for JSON that contain newlines or quotes
  • Escaping user input for use in string literals
  • Debugging escaped strings by converting them back to readable form
  • Copying text from code that contains escape sequences
  • Working with shell scripts where quotes and backslashes need escaping
  • Preparing regex patterns that contain special characters

How It Works

Escape Mode (Default)

Converts these characters to escaped sequences:
  • \\\ (backslash must be escaped first)
  • \n (newline) → \\n
  • \r (carriage return) → \\r
  • \t (tab) → \\t
  • "\\"
  • '\\'

Unescape Mode

Reverses the process, converting escape sequences back to their literal characters.
The order of operations matters: when escaping, backslashes are replaced first to avoid double-escaping. When unescaping, backslashes are replaced last.

Input Format

Escape: Plain text with special characters
Hello
World	"Test"
Unescape: Text with backslash escape sequences
Hello\nWorld\t\"Test\"

Output Format

Escape Output:
Hello\nWorld\t\"Test\"
Unescape Output:
Hello
World	"Test"

Examples

Input:
Line 1
Line 2
Line 3

Output:
Line 1\nLine 2\nLine 3

Technical Details

Located in lib/tools/engine.ts:469-470
The tool implements two functions:
  • escapeBackslash (engine.ts:105-113): Replaces special characters with escape sequences
  • unescapeBackslash (engine.ts:115-123): Converts escape sequences back to characters

Supported Escape Sequences

CharacterEscape SequenceDescription
\\\Backslash
Newline\nLine feed (LF)
Carriage return\rCR character
Tab\tHorizontal tab
Double quote\"Quote character
Single quote\'Apostrophe

Performance

  • Synchronous processing: Instant client-side conversion
  • Sequential replacements: Uses replaceAll for efficiency
  • No regex parsing: Simple string replacement for maximum speed
This tool handles only the most common escape sequences. It does not support Unicode escape sequences (\u0041), octal escapes (\101), or hex escapes (\x41). For those, use language-specific parsers.

Common Patterns

JSON String Preparation

When embedding multi-line text in JSON:
{
  "message": "Line 1\nLine 2\nLine 3"
}

Shell Script Escaping

For bash/zsh scripts with quotes:
echo "She said, \"Hello\" and left"

Programming Language Strings

For JavaScript, Python, Java, C++, etc.:
const text = "First line\nSecond line\tTabbed";

Build docs developers (and LLMs) love