Escape / Unescape

Escape or unescape special characters in text


1. What are Escape Sequences?

Escape sequences are special character combinations that represent characters which cannot be directly typed or would have special meaning in text. They start with a backslash (\) followed by a character code, allowing you to include special characters like newlines, tabs, and quotes in strings.

2. How does it work?

Escaping replaces special characters with their escape sequence equivalents (e.g., a newline becomes \\n). Unescaping reverses this process, converting escape sequences back into their actual characters. This is essential for programming when you need to include special characters in strings, especially for JSON, JavaScript, or text containing quotes and line breaks.

Common Escape Sequences

  • \\n: Newline
  • \\r: Carriage return
  • \\t: Tab
  • \\": Double quote
  • \\': Single quote
  • \\\\: Backslash

3. Examples

Newline escape

Line 1
Line 2 → Line 1\nLine 2

Quote escape

He said "Hello" → He said \"Hello\"

References