Javascript Escape / Unescape
Escape or unescape JavaScript strings.
Loading…
About Javascript Escape / Unescape
Dropping user text or a long snippet into a JavaScript string goes wrong in several ways at once: a stray quote ends the literal, a backtick or ${ fires a template, and a closing script tag can end an inline script early. Escape rewrites all of those, plus control characters, so the result works in a single-quoted, double-quoted or backtick string. Unescape decodes the common sequences back to text without evaluating anything.
Features
- Escapes backslashes, single and double quotes and backticks
- Escapes ${ so template literals do not interpolate
- Writes < and > as \x3C and \x3E, so a script end tag cannot close an inline script
- Control characters become \n, \t or \xHH, and U+2028 and U+2029 become \u2028 and \u2029
- Unescape decodes \xHH, \uHHHH, \u{code point} and \0 without running any code
Limitations
- Non-ASCII characters such as é and emoji are left as they are, not converted to \u sequences.
- Regular expression characters such as . and * are not escaped, so the output is for strings, not for building a pattern.
How it works
- Paste the text or the escaped string into the JAVASCRIPT Input panel.
- Select Escape or Unescape with the toggle in the middle column.
- Read the JAVASCRIPT Output panel, which updates as you type.
- Select Copy Output, then wrap the result in whichever quote style your code uses.
Example input
She said "hello"
New line with a \ backslashFrequently asked questions
- Yes. Angle brackets are written as hex escapes, so a closing script tag inside your text cannot end the block early. Add your own quotes around the result and it can sit directly in the page.
- No. It only replaces escape sequences with the characters they stand for, using pattern matching, never eval. A malformed \x or \u sequence is left as typed, and an unknown escape like \q simply drops its backslash.
- Any of the three. Single quotes, double quotes and backticks are all escaped, so the same output is valid inside each kind of string literal. Choose whichever your style guide prefers.
- U+2028 and U+2029 are line and paragraph separators. Before 2019, JavaScript did not allow them raw inside string literals, so the tool writes them as escapes to keep the code valid in older engines.
