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
- Pick a context from the Target dropdown: JSON, JavaScript, HTML, URL, Shell or Regex.
- Choose Escape or Unescape; Unescape is disabled for Shell and Regex, which are escape only.
- Paste or type your string into the Input box.
- 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>
<a href="?x=1&y=2">Tom & Jerry</a>
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 (&, <, >, " and ') so the text is safe inside HTML, and unescape converts them, plus ', 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
- Escaping the same string for JSON, HTML, URLs and shells
The same character means different things in JSON, HTML, URLs, shells and regex. A tour of each context's escape rules and why they differ.
Related tools
JSON Escape / Unescape
Escape a string so it is safe inside JSON, or decode JSON escape sequences back to plain text. Handles quotes, backslashes, newlines, tabs and unicode.
HTML Entity Encoder
Encode text to HTML entities online. Escape &, <, >, " and ' so markup renders safely, with an option to convert non-ASCII characters too. Free and private.
URL Encoder
Encode or decode URLs and query strings online. Percent-encode a value, escape a full URL, or decode back to plain text. Free, fast and private.
.env to JSON
Convert a .env file to JSON, or JSON back to .env. Parses KEY=value lines, comments, quotes and export. Runs entirely in your browser.
ASCII Table
Full ASCII table for all 128 codes with decimal, hex, octal and binary values, character names and descriptions. Search by code, hex or character.
Aspect Ratio Box Generator
Generate CSS for a responsive aspect-ratio container. Use the modern aspect-ratio property or the padding-top fallback, then copy the ready code.