Add Backslashes
Add backslashes to escape special characters in strings for JavaScript, JSON, RegExp, and other contexts. Paste text and get escaped output instantly.
About
Unescaped strings cause silent failures. A single unescaped double quote inside a JSON payload breaks the parser. An unescaped backslash in a JavaScript string literal produces unexpected characters or syntax errors. This tool applies correct escape sequences character-by-character across three contexts: JavaScript/JSON (escaping ", \, newlines, tabs, and control characters per ECMA-262 Β§12.8.4), Regular Expressions (escaping the 12 metacharacters defined in the RegExp syntax), and a raw mode that simply prefixes every occurrence of \ with an additional \. The reverse operation (unescape) is also supported. Note: this tool handles standard ASCII and Unicode BMP escaping. Surrogate pairs for characters outside the BMP are not decomposed into \uXXXX sequences.
Formulas
The escape operation maps each input character c to its escaped form cβ² using a lookup table M specific to the selected mode.
The full output string Sβ² is the concatenation of all transformed characters:
Where n = length of input string, and the product symbol here denotes string concatenation. For JS/JSON mode, the map M contains 10 entries: \ β \\, " β \", " β \", newline β \n, carriage return β \r, tab β \t, backspace β \b, form feed β \f, forward slash β \/, and null byte β \0. For RegExp mode, the map escapes 12 metacharacters: . * + ? ^ $ { } ( ) [ ] | \. The unescape operation applies the inverse map Mβ1, parsing two-character escape sequences back to their original single characters.
Reference Data
| Character | Name | JS/JSON Escape | RegExp Escape | Unicode Escape | Hex Escape |
|---|---|---|---|---|---|
| " | Double Quote | \" | " (no escape needed) | \u0022 | \x22 |
| " | Single Quote | \" | ' (no escape needed) | \u0027 | \x27 |
| \ | Backslash | \\ | \\ | \u005C | \x5C |
| / | Forward Slash | \/ | \/ | \u002F | \x2F |
| (newline) | Line Feed | \n | \n | \u000A | \x0A |
| (return) | Carriage Return | \r | \r | \u000D | \x0D |
| (tab) | Horizontal Tab | \t | \t | \u0009 | \x09 |
| (backspace) | Backspace | \b | \b (word boundary in regex) | \u0008 | \x08 |
| (form feed) | Form Feed | \f | \f | \u000C | \x0C |
| . | Dot / Period | . (no escape needed) | \. | \u002E | \x2E |
| * | Asterisk | * (no escape needed) | \* | \u002A | \x2A |
| + | Plus | + (no escape needed) | \+ | \u002B | \x2B |
| ? | Question Mark | ? (no escape needed) | \? | \u003F | \x3F |
| ^ | Caret | ^ (no escape needed) | \^ | \u005E | \x5E |
| $ | Dollar Sign | $ (no escape needed) | \$ | \u0024 | \x24 |
| { | Left Curly Brace | { (no escape needed) | \{ | \u007B | \x7B |
| } | Right Curly Brace | } (no escape needed) | \} | \u007D | \x7D |
| ( | Left Parenthesis | ( (no escape needed) | \( | \u0028 | \x28 |
| ) | Right Parenthesis | ) (no escape needed) | \) | \u0029 | \x29 |
| [ | Left Square Bracket | [ (no escape needed) | \[ | \u005B | \x5B |
| ] | Right Square Bracket | ] (no escape needed) | \] | \u005D | \x5D |
| | | Pipe | | (no escape needed) | \| | \u007C | \x7C |