This string escape unescape tool converts text to and from four common formats: a JSON string, a JavaScript string, HTML entities and URL percent-encoding. Pick the format, choose Escape or Unescape, paste your text and press Convert. It saves you from counting backslashes by hand.
Escaping is the step that makes text safe to place inside another format. A quote inside a string, an angle bracket in a web page or a space in a web address would all break something if left as they are. Unescaping goes the other way and gives back the original text. All the work is done in your browser.
How to use string escape unescape
- Choose the format: JSON, JavaScript, HTML or URL.
- Choose Escape to make text safe, or Unescape to read escaped text.
- Set the options that appear for that format.
- Paste your text and press Convert.
- Copy the result, or press "Use result as input" to run it back the other way.
JSON string escaping
A JSON string is wrapped in double quotes. The characters that must be escaped are the double quote, the backslash and the control characters from U+0000 to U+001F [1]. There are short forms for the common ones, such as \n for a new line, \t for a tab and \" for a quote, and any character may be written as \uXXXX with four hex digits [1]. A character outside the Basic Multilingual Plane is written as a pair of \u escapes, for example \uD834\uDD1E [1]. The option "escape non-ASCII too" writes every non-ASCII character that way. When you unescape, you can paste the text with or without its surrounding quotes.
JavaScript string escaping
In a JavaScript string the backslash starts an escape sequence [2]. The table lists the main forms. A backslash before any other character is an identity escape, so \z means z [2]. The escaper writes both kinds of quote and the backtick, so the result works inside single quotes, double quotes or a template literal. It also writes ${ as \${ so a template literal does not run it as code [2].
| Escape | Meaning |
|---|---|
| \n \r \t | Line feed, carriage return, tab |
| \xHH | One character from U+0000 to U+00FF |
| \uHHHH | One UTF-16 code unit |
| \u{H...} | Any code point up to 10FFFF |
| \0 | The null character |
When you unescape, a backslash followed by a line break is removed, because that is how JavaScript continues a string onto the next line [2].
HTML entities
In HTML a character reference starts with an ampersand and ends with a semicolon. It can be a name such as <, a decimal number such as < or a hexadecimal number such as < [3]. A less-than sign must be written as < in normal text, and a quote cannot sit inside an attribute value that uses the same quote [3]. The escaper writes &, <, >, " and '. The unescaper reads those, every numeric reference and a built-in list of common names such as ©, € and —. A name that is not in the list is left as it is.
URL percent-encoding
For URLs the tool uses the encodeURIComponent and encodeURI functions. encodeURIComponent keeps only letters, digits and the marks - _ . ! ~ * ' ( ) and encodes everything else, including / ? : @ & = + $ and # [4]. That makes it the right choice for one query value. encodeURI leaves those separators alone, so it suits a whole address. Text that contains a lone surrogate cannot be encoded and gives an error [4]. When you unescape, the option "treat + as space" is for form-style query strings.
Worked example
The text He said "hi" and left, with a new line after it, becomes He said \"hi\" and left.\n in JSON mode. The text Tom & Jerry becomes Tom & Jerry in HTML mode. The text a b&c becomes a%20b%26c in URL component mode.
Common mistakes
- Escaping twice. Escaped text escaped again has doubled backslashes or entities.
- Using the whole-address mode for a single value. A value containing & or = needs the component mode.
- Pasting text with a stray backslash in unescape mode. A backslash before u or x must be followed by hex digits.
- Expecting every named HTML entity. Numeric references always work.
When to escape and when to unescape
Escape when you are about to place text inside something else. Put a message into a JSON body, build a string for a script, show user text on a web page or add a value to a link. Unescape when you receive text in that form and want to read it, for example a log line full of \n and \" or an address full of %20 and %2F.
The "Use result as input" button swaps the direction and moves the result into the input box. Press it to check that a round trip returns your original text. If it does not, one of the two steps used the wrong format.
Escaping is not the same as encoding for safety. Escaping HTML text helps stop markup from being read as code, but where the text goes decides which rules apply. A value placed in a script, a style sheet or an address needs the escaping for that place.
What next
Use this often? Add String Escap to your Home screen.
Frequently asked questions
What does string escape unescape mean?
Escaping rewrites special characters so the text is safe inside a format such as JSON, JavaScript, HTML or a URL. Unescaping turns the escaped text back into the original.
How do I escape a JSON string?
Choose JSON and Escape, paste the text and press Convert. Quotes, backslashes and control characters get backslash escapes.
What is the difference between URL component and whole address mode?
Component mode encodes separators such as slash, question mark and ampersand, so use it for one value. Whole address mode leaves them as they are.
Does it decode every HTML entity name?
It decodes all numeric references and a list of common names such as copy, euro and mdash. Other names are left unchanged.
Is my text uploaded anywhere?
No. All conversion runs in your browser.
Sources
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.