What string escaping is

Escaping rewrites the characters that are special in a format — quotes, backslashes, angle brackets, commas — so they are read as ordinary data instead of structure. Every format has its own rules, which is why this tool offers eight of them and why picking the right one matters more than the escaping itself.

Almost every text format reserves a few characters for its own grammar. In JSON, a double quote ends a string. In HTML, a less-than sign opens a tag. In a CSV file, a comma starts the next cell. The moment your actual data contains one of those characters, the file becomes ambiguous: is that quote the end of the string, or part of the message? Escaping is the agreed answer — a set of replacement spellings that let the special character travel through the format as data. A quote inside a JSON string is written as backslash-quote, a less-than sign in HTML text is written as an entity, and a comma inside a CSV field is neutralised by wrapping the whole field in quotes.

Two properties make escaping trustworthy. First, it is lossless: unescaping the output always returns the exact original text, character for character, which you can test on this page with the Swap button. Second, it is context-specific: a string escaped for JSON is not escaped for HTML, and neither one is escaped for a URL. Most real-world escaping bugs — and a good share of security holes — come from applying the right escaping in the wrong place, or from escaping twice and corrupting the data. The sections below walk through each format this tool handles, what it escapes, and where its output belongs.

One honest limit before the detail: this tool performs the mechanical rewriting only. It does not know where your string will end up, and escaping by itself does not sanitise untrusted input or make it safe. The final section of this guide explains that distinction in full, because it is the one that causes real damage when it is misunderstood.

Which format do you need?

Quick answer: match the format to the destination of the string. JSON for JSON data, JavaScript for a JS string literal, HTML entities for text inside a web page, XML for XML documents, URL (component) for one value inside a web address, CSV field for one spreadsheet cell, Regex for literal text in a pattern, and Unicode when only ASCII is allowed.
FormatUse it when the string goes into…Characters escapedExample
JSON stringa JSON value between double quotes" \ and control characters (newline becomes \n)say \"hi\"
JavaScript stringa quoted string literal in JS code" ' \ and control charactersit\'s
HTML entitiestext content or attribute values in a page& < > " '&lt;tag&gt;
XMLtext or attribute values in an XML document& < > " ' (as &apos;)&lt;tag&gt;
URL (component)one query value or path segment in a URLeverything except A–Z a–z 0–9 - _ . ! ~ * ' ( )a%20b%26c
CSV fieldone cell in a comma-separated rowfield wrapped in quotes if it holds , " or a line break; quotes doubled"a, b"
Regex specialsliteral text inside a regular expression. * + ? ^ $ { } ( ) | [ ] \3\.50
Unicode \uXXXXsystems that accept ASCII onlynon-ASCII and control characters\u00e9

The table also shows why a single "escape" button could never be enough: the same input produces eight different correct outputs, each wrong in the other seven contexts. A price of 3.50 needs its dot escaped in a regex (where a dot means "any character") and needs nothing at all in JSON. An ampersand needs escaping in HTML, XML and a URL component, and is an ordinary character in a JSON string. When you are unsure which context applies, find out before you escape — the destination decides, not the data.

JSON string escaping

JSON strings must escape the double quote, the backslash and every control character. A newline becomes \n, a tab becomes \t, and any other control character becomes \u00XX. This tool's JSON mode follows RFC 8259 exactly, using the same rules as the browser's own JSON.stringify.

JSON is the data format of the modern web, and its string rules are deliberately small. Inside a JSON string, exactly three kinds of character cannot appear raw: the double quote that would end the string, the backslash that starts an escape, and the control characters U+0000 to U+001F, which include the line break and tab. Everything else — accented letters, emoji, angle brackets, ampersands — may legally appear as itself, and this tool leaves it that way. Minimal escaping keeps JSON readable and keeps the output short.

The worked example from the workbench shows all three rules at once. Take the text He said "hi"\ followed by a line break. The quote becomes backslash-quote, the trailing backslash doubles itself, and the line break becomes backslash-n, giving the literal sequence He said \"hi\"\\\n. Paste that into Unescape and you get the original back, exactly. Unescape is strict because JSON itself is strict: a sequence such as \x or a lone backslash is not "close enough", it is invalid JSON, and the tool says so rather than inventing a meaning for it.

A common confusion is between escaping a string and producing JSON. This mode escapes the content of one string — you supply the surrounding quotes yourself when you assemble the document. If you paste a complete JSON document, quotes and all, the tool will escape it as if it were plain text, which is rarely what you want. For validating and pretty-printing whole documents, FreeSoftis has a separate JSON Formatter; this page is for the single-string job.

JavaScript string escaping

JavaScript mode escapes both quote types, the backslash and control characters, so the output can sit safely inside a single-quoted or double-quoted literal. It is the escaping you need when building strings by hand for code, templates or configuration files.

JavaScript string literals look like JSON strings but live under looser, older rules. Both ' and " can delimit a string, so text containing an apostrophe — it's — breaks a single-quoted literal unless the apostrophe is escaped. This mode escapes both quote characters, which makes its output safe inside either delimiter, at the cost of an occasional backslash a human would not have typed. Machines do not mind, and correctness beats elegance when strings are generated.

The unescape direction understands the full everyday set: the simple escapes \n \r \t \b \f \v \0 \\ \' \" \/, hexadecimal \xHH, unicode \uXXXX, and the code-point form \u{...} that JavaScript uses for characters beyond the basic plane. An emoji, for instance, can be written \u{1F600}; unescaping it here produces the emoji itself. Unknown escapes are errors, not guesses — \q has no meaning in a JavaScript string literal's escape table (in real code it silently means just q, which is exactly the kind of quiet data corruption this tool refuses to perform).

Template literals — the backtick strings — are a different context again: inside them, ${ starts an interpolation, so generated text containing ${ needs its own treatment, which no generic string escaper can decide for you. If you are building JavaScript by concatenating escaped strings, prefer your language's JSON encoder for data and keep code and data separate; escaping is a bridge, not an architecture.

HTML and XML escaping

HTML and XML escaping replaces the five markup characters: & becomes &amp;, < becomes &lt;, > becomes &gt;, " becomes &quot;, and ' becomes &#39; in HTML or &apos; in XML. The ampersand must always be escaped first — it is the character that starts every entity.

When text is placed inside a web page, the browser's parser cannot tell your data from the page's own markup. The sentence "the <a> tag" would vanish into an actual link element if written raw. Entity escaping solves it by giving each dangerous character a name the parser recognises as data: the worked example <a href="x">& becomes &lt;a href=&quot;x&quot;&gt;&amp;, which a browser displays as the original text, exactly. This is the single most-used escaping on the web — every template engine and framework performs it automatically when it inserts text into a page.

Unescaping resolves the five core entities, decimal references such as &#169;, hexadecimal references such as &#xA9;, and the common named entities — &copy;, &nbsp;, &mdash;, &eacute; and their relatives from the HTML standard's table. It is deliberately not unlimited: an unknown name such as &banana; produces an error, because in a real page that text signals a bug, and silently leaving it in place would hide the bug. The XML unescaper is stricter still, as XML itself is: XML defines only the five core entities, so &copy; is an error there even though browsers accept it in HTML.

Escaping for HTML has one more subtlety this tool is honest about: it covers text content and quoted attribute values. It does not make a string safe inside a <script> block, an event handler, or an unquoted attribute — those are different contexts with different rules, covered in the security section below. Context is everything, in markup most of all.

URL component encoding

URL (component) mode percent-encodes one value so it can sit inside a web address: a space becomes %20, an ampersand %26, an equals sign %3D. It is the encodeURIComponent behaviour — the structural characters of the URL are encoded too, so the value cannot break the address apart.

A URL is a string with a grammar: slashes separate path segments, a question mark opens the query, ampersands separate parameters and equals signs bind names to values. A search term like "fish & chips" contains a character with a job in that grammar, so before it can travel as data it must be percent-encoded — each unsafe byte written as a % followed by two hex digits. Non-ASCII characters are encoded byte by byte from their UTF-8 form, which is why é becomes the two sequences %C3%A9.

Choose this mode for a single value: a query parameter, a redirect target, a filename in a path. For tidying a complete address while keeping its structure readable, the Full URL method of FreeSoftis's URL Encoder & Decoder is the right tool instead — encoding a whole URL with the component method would encode its own :// and slashes and break it. Decoding here is strict for the same reason JSON decoding is strict: a % that is not followed by two hex digits, or a multi-byte sequence cut in half, has no valid meaning, and the tool reports it instead of guessing.

CSV field escaping

A CSV field only needs protection when it contains a comma, a double quote or a line break. Then the whole field is wrapped in double quotes and every internal quote is doubled, as RFC 4180 specifies. A field with none of those characters is returned unchanged — that is correct CSV, not a missing feature.

CSV looks simple and causes a disproportionate share of data bugs, usually at exactly this point. The name Smith, John "Jack" contains both a comma and quotes; written raw into a row, a spreadsheet would read it as two cells and a syntax error. Escaped, it becomes one field: "Smith, John ""Jack""" — wrapped, with each real quote doubled. The doubling rule is the part people forget: inside a quoted field, two quotes in a row mean one literal quote, and a single quote followed by a comma or a line break ends the field.

Unescaping reverses a single field. If the value starts with a quote, the tool parses it properly: doubled quotes collapse to one, and the field must end cleanly at its closing quote — characters after it, or a missing closing quote, are errors worth surfacing, because in a real file they mean a row is misaligned and every cell after it would shift. If the value does not start with a quote, it is already a plain field and is returned as-is. Note what this tool does not do: it handles one field at a time, not whole files with their rows and headers — for a full file, your spreadsheet's own import and export does this escaping automatically.

Regex and Unicode escapes

Regex mode backslash-escapes the fourteen characters that are special in a regular expression — . * + ? ^ $ { } ( ) | [ ] and the backslash itself — so pasted text matches literally. Unicode mode writes every non-ASCII or control character as \uXXXX, so café becomes caf\u00e9.

The regex failure this prevents is painfully common: you paste a filename, a price or a version number into a pattern and it matches the wrong things, because a dot matches any character, a plus repeats the previous one, and brackets open a character class. The text 3.50 as a raw pattern also matches 3x50 and 3250. Escaped to 3\.50 it matches only itself. The same applies to file paths full of backslashes and to text containing parentheses or a pipe. Escape first, then add the pattern syntax you actually intend around the literal text.

Regex unescaping is intentionally limited, and the tool says so on its face. A backslash in a pattern is ambiguous — \. is an escaped dot, but \d is a digit class and \w a word class; neither was ever a literal character, so "unescaping" them into d and w would corrupt the pattern. This tool therefore only removes the punctuation escapes it creates, and reports an error for anything else rather than silently rewriting your regex.

Unicode escaping serves the opposite audience: systems that only accept ASCII — some configuration formats, older protocols, certain code generators, and log files that must survive any encoding. Every character outside printable ASCII is written as \uXXXX using its UTF-16 code unit, so an emoji appears as a surrogate pair (\uD83D\uDE00 for 😀), which is exactly the form Java and JavaScript use natively. Unescaping reverses \uXXXX sequences and leaves ordinary characters untouched, and a malformed sequence — fewer than four hex digits — is an error, because guessing would risk merging it with the text that follows.

Worked examples

Verified examples — each of these was tested against this tool's own conversion code before publication. Escape and Unescape are exact inverses for every row.
FormatInputEscaped output
JSON stringHe said "hi"\ + a line breakHe said \"hi\"\\\n (literal backslashes)
HTML entities<a href="x">&&lt;a href=&quot;x&quot;&gt;&amp;
Unicodeé\u00e9
CSV fieldSmith, John "Jack""Smith, John ""Jack"""
URL (component)a b&c=da%20b%26c%3Dd
Regexprice: $3.50 (sale)price: \$3\.50 \(sale\)

Reading the JSON row takes care, because the output is itself full of backslashes: the original quote gains one backslash, the original backslash becomes two, and the line break becomes backslash-n — three backslashes and an n at the end, each doing a different job. This is why hand-escaping JSON is so error-prone and why a tool should do it: humans reliably lose count of backslashes, and a single missing one makes the whole document invalid.

Why unescaping fails

Unescape errors are information, not malfunctions. Each format has a grammar, and input that breaks it — a bad hex digit, an unknown entity, an unterminated quote — has no single correct decoding. This tool explains the specific problem instead of producing plausible-looking garbage.

Silent guessing is how data gets corrupted. If an unescaper "helpfully" decoded \u12G4 by treating G as zero, the output would look fine and be wrong, and the error would surface much later, somewhere expensive. The failures you will meet here fall into a few families. Incomplete sequences: a \u with two digits instead of four, a % with one, an entity with no semicolon, a CSV quote that never closes. Unknown sequences: an entity name that does not exist, a regex escape that is really a character class, a JSON escape JSON does not define. And misplaced content: characters after a CSV field's closing quote, or a raw line break inside JSON string content, which JSON forbids outright.

In almost every case the cause is the same: the text was copied incompletely, or it was escaped for a different format than the one selected. Check the tail of the text first — truncation is the most common culprit — then check that the format matches the source. If text was escaped twice (it happens constantly with URLs), unescape it once, inspect the result, and unescape again only if it still contains escapes.

Escaping is not sanitising

This tool does not make untrusted input safe, and no escaper can. Escaping is a formatting step for a known destination. Security comes from using the right encoding for the exact context, plus validation and parameterised APIs — as the OWASP cheat sheets cited below describe.

It would be easy — and wrong — to market an escape tool as a security tool. Here is the honest position. Output encoding is a genuine and necessary layer of defence against injection: the OWASP Cross Site Scripting Prevention Cheat Sheet recommends it explicitly. But it only works when the encoding matches the exact context the data lands in — HTML body, HTML attribute, JavaScript, URL and CSS are all different contexts with different rules, and this page has no way of knowing which one your string is heading for. A JSON-escaped string pasted into a web page is not HTML-safe. An HTML-escaped string inside a script block is not JavaScript-safe.

Escaping also does nothing about the rest of input safety: it does not validate that an email address is an email address, does not parameterise a database query, does not check a file type, and does not remove anything malicious — by design, because its job is to preserve your data exactly, reversibly. Treat this tool as what it is: a precise, private, mechanical converter for people who know their destination format. If you are handling untrusted input in production, use your framework's context-aware encoding and follow the OWASP guidance linked below — do not hand-escape and hope.

A final privacy note that applies to every format on this page: the conversion happens entirely in your browser, using JavaScript functions built into the browser itself. Your text is never uploaded, transmitted or stored, which makes the tool safe for strings you would not paste into a server-side converter — tokens, private notes, internal code. Nothing is kept for you either: refresh the page and the boxes are empty, so copy or download anything you need first.

Sources & References

Every source below was checked live in November 2026 and returned a readable page at the time of checking.