Boneyard Tools

String Escape and Unescape

Paste a string and convert it for a specific context: a JSON or JavaScript string literal, HTML text, a URL component, a POSIX shell argument or a regular expression. Each target applies the correct rules, such as doubling backslashes for JSON, percent encoding for a URL or single-quote wrapping for the shell. For JSON, JavaScript, HTML and URL you can also switch to unescape and recover the original string exactly.

How to escape a string

  1. Pick a context from the Target dropdown: JSON, JavaScript, HTML, URL, Shell or Regex.
  2. Choose Escape or Unescape; Unescape is disabled for Shell and Regex, which are escape only.
  3. Paste or type your string into the Input box.
  4. Read the transformed string in the Result box and copy it.

Examples

Escape for a JSON or JavaScript string literal

He said "hi"
bye
He said \"hi\"\nbye

Escape for HTML text

<a href="?x=1&y=2">Tom & Jerry</a>
&lt;a href=&quot;?x=1&amp;y=2&quot;&gt;Tom &amp; Jerry&lt;/a&gt;

Escape for a regular expression

1.5 + 2
1\.5 \+ 2

Frequently asked questions

Which contexts can I escape for?

JSON, JavaScript, HTML, URL, shell and regular expressions. Each uses the rules for its context, such as doubling backslashes for JSON, entity encoding for HTML, percent encoding for URLs and single-quote wrapping for the shell.

How are control characters like newlines and tabs handled for JSON and JavaScript?

Newline, carriage return, tab, backspace and form feed become the short escapes \n, \r, \t, \b and \f. Any other control character below code point 32 is written as a \uXXXX sequence, so the result is a safe string literal with no literal control bytes.

How is the JSON or JavaScript output formatted?

It is the contents of a string literal without the surrounding quotes, so you can drop it straight between your own quotes in code or config. JSON and JavaScript share the same escape set here, so the same unescape recovers either one.

How are HTML special characters handled?

The five core characters &, <, >, double quote and single quote are converted to entities (&amp;, &lt;, &gt;, &quot; and &#39;) so the text is safe inside HTML, and unescape converts them, plus &apos;, back to the original characters.

Does the URL option encode the whole address?

It uses encodeURIComponent, which is meant for a single component such as a query value. It percent encodes reserved characters like &, = and spaces but leaves letters, digits and a few marks like - _ . ! ~ * ' ( ) alone, so do not run a full URL through it or the slashes and colon will be encoded.

How does the shell escaping work and is it safe everywhere?

It wraps the whole value in single quotes and rewrites any embedded single quote as the sequence '\'' so the argument is safe to paste into a POSIX shell like bash, sh or zsh. It is not designed for Windows cmd or PowerShell, which quote arguments differently.

What does the regex escaping cover?

It backslash-escapes the characters that are special in a JavaScript regular expression: . * + ? ^ $ { } ( ) | [ ] the backslash, the forward slash and the hyphen. After escaping, the text matches literally instead of being interpreted as a pattern.

Why can I not unescape shell or regex output?

Shell quoting and regex escaping are not losslessly reversible from the output alone, so the Unescape option is disabled for them and offered only for JSON, JavaScript, HTML and URL, where the original string can be reconstructed exactly.

Is my text uploaded?

No. All escaping and unescaping happens locally in your browser, so your strings never leave your device.

Learn more

Related tools