Skip to content
Buy me a coffee
String Escaper & Utilities

JavaScript Escape / Unescape

Escapes or unescapes a JavaScript string, replacing characters that could prevent interpretation with their escape sequences.

Ctrl + Enter

JavaScript string escaping explained

Why escape?

JavaScript string literals can be delimited by single quotes, double quotes or backticks. Whichever delimiter you pick, a literal occurrence of that delimiter, a backslash or a raw line break inside the value breaks the literal. Escaping produces text that is safe to paste between either kind of quote, which is why this tool escapes both quote characters.

Rules applied by this tool

CharacterEscaped asNotes
Horizontal tab (U+0009)\t
Vertical tab (U+000B)\v
Null character (U+0000)\0
Backspace (U+0008)\b
Form feed (U+000C)\f
Newline (U+000A)\n
Carriage return (U+000D)\r
Single quote\'
Double quote\"
Backslash\\Handled first.
Line separator / paragraph separator (U+2028, U+2029)\u2028, \u2029Historically illegal inside string literals; still escaped for safety.
Other control characters\x00\x1F, \x7F
Non-ASCII characters\u00E9, surrogate pairs for emojiOnly with the "\uXXXX" option.

Before / after

InputEscaped output
It's a "test"It\'s a \"test\"
C:\temp\newC:\\temp\\new
Two lines separated by a line breakfirst\nsecond
Café 😀 (with the \uXXXX option)Caf\u00E9 \uD83D\uDE00

Unescaping

The unescape button decodes every sequence above plus \xXX, \uXXXX, \u{X…} (ES2015 code point escapes), legacy octal escapes (\101) and line continuations (a backslash immediately followed by a line break, which produces nothing). An unknown escape such as \q simply yields q, which is what JavaScript itself does.

Escaping is not sanitizing

Escaping quotes makes a value safe inside a string literal; it does not make it safe to inject into HTML or into eval(). To embed data in a page, serialize it with JSON.stringify() and additionally replace < with \u003c so that a value containing </script> cannot close your script tag. In modern code, template literals and String.raw reduce the need for manual escaping considerably.

const s = 'It\'s a "test"';                  // escaped literal
const safe = JSON.stringify(userInput).replace(/</g, '\\u003c');
const path = String.raw`C:\temp\new`;         // no escaping needed