JS Escape / Unescape
Safely escape or unescape special characters for JavaScript strings
Make any text safe to drop inside a JavaScript string
A stray double quote, a line break or a backslash inside a string literal is enough to break a script. This tool converts raw text into an escaped form you can paste between quotes, and it can reverse the process when you have an escaped string and want to read what it actually says. It runs in your browser: you paste text, press a button, and the result appears below the form.
How to escape or unescape a string
- Paste your text into the Input String / Code box, or press Sample to load an example containing a quote and a backslash.
- Choose Escape (To JS Safe) or Unescape (To Plain) in the Action menu.
- Pick an Encoding Type: Standard, Hexadecimal or Unicode.
- Click Transform String. The Transformed Output panel opens with a Copy button.
Editing the input hides the previous result, so you never copy stale output by accident. Paste reads from your clipboard and Clear empties the field.
What the three encoding types do
- Standard uses the same escaping rules as JSON string encoding: quotes become \", backslashes are doubled, and newlines and tabs turn into \n and \t.
- Hexadecimal leaves plain ASCII characters alone and rewrites anything above code 127 as \xHH. This is only meaningful for characters up to code 255, so emoji and most non-Latin text are better handled by the Unicode option.
- Unicode rewrites every character, including letters and digits, as a \uHHHH sequence. Characters outside the basic plane come out as two surrogate escapes, which is how JavaScript represents them.
Where this helps
- Embedding a multi-line error message or HTML snippet in a JavaScript variable without hand-escaping each line.
- Reading a log entry or API response where text arrives as escaped sequences and you want the human version.
- Storing text with accented or non-Latin characters in a source file that must stay pure ASCII.
- Preparing a string for a JSON config, since Standard mode follows JSON conventions.
Limits worth knowing
Unescape works by wrapping your text in quotes and parsing it as a JSON string, so it understands \n, \t, \\, \" and \uHHHH. Hex sequences such as \x41 and single-quote escapes like \' are not valid JSON and will produce the message Invalid escape sequence. If you see that message, check for those forms or for a lone backslash at the end of the text. Also note that Hexadecimal mode is not reversible with the Unescape action for the same reason, so use Standard or Unicode when you plan to round-trip.
Related Tools