Copied!
Back
Text Tool

String Escape & Unescape Tool

Escape and unescape strings online for JSON, HTML, JavaScript, URL, CSV and SQL. This free tool handles backslash escaping, escape sequences for newline, tab and quotes, HTML entities, URL percent-encoding, and SQL injection prevention. Paste any string to instantly see the escaped or unescaped result. Supports Unicode \uXXXX notation, special characters, and all common escape formats. All processing happens client-side in your browser — nothing is uploaded to a server.

Last updated: May 2026 · Reviewed by FreeDevTool security engineering team
string-escape.tool
0 characters
0 characters
SequenceCharacterDescription
\\\Backslash
\""Double quote
\''Single quote
\nNewline (line feed)
\rCarriage return
\tHorizontal tab
\bBackspace
\fForm feed
\0Null character
\uXXXXUnicode code point (hex)

The complete guide to string escaping — JSON, HTML, JavaScript, URL, CSV, and SQL

Every string-related security incident I have read in the past decade — Equifax, MOVEit, Log4Shell, every weekend's fresh round of XSS reports — traces back to a string that crossed a context boundary without being re-escaped for the new context. Escaping is not boilerplate; it is the rule that keeps user data inside its data envelope and prevents it from being interpreted as code. This guide covers each escaping context the tool supports — what the escape sequences are, what the parser is actually defending against, and the language-level functions you should reach for instead of writing your own.

Context is everything — the same string needs different escaping in different places

The single biggest concept to internalise is context-specific escaping. A string flowing from a database into an HTML page should be HTML-escaped at the moment it is written into HTML. The same string flowing into a JavaScript string literal needs JS-string escaping. The same string going into a URL parameter needs percent-encoding. None of these escapings substitute for each other:

Where the string landsEscape withWhat it prevents
HTML element body (<p>…</p>)HTML entity escape & < >HTML/XSS injection
HTML attribute valueHTML entity escape + always quote attributeAttribute breakout, event handlers
JavaScript string literalJS escape (\\ \" \n \uXXXX)String breakout, code injection
URL query parameterpercent-encoding (encodeURIComponent)Param injection, broken parsers
JSON valueJSON escape (JSON.stringify)JSON parse errors, injection
CSV cellWrap in "…", double internal ""Cell breakout, CSV injection
SQL valueDon't escape — use a parameterised querySQL injection
Shell commandDon't escape — use exec with arg arrayCommand injection

The two "don't escape" rows in that table are deliberate — they are the two places where ad-hoc escaping is the wrong tool. We will come back to them.

JSON escaping — what RFC 8259 actually requires

RFC 8259 (the current JSON spec) requires escaping just six characters inside string values: ", \, and the U+0000–U+001F control range. Everything else is allowed verbatim, including non-ASCII. Most JSON encoders also escape / defensively (so the string </script> doesn't break out of an inline JSON-in-HTML block), and many escape all non-ASCII to \uXXXX for transport safety:

The simple rule in JavaScript: if you can use JSON.stringify(value), do that. It is correct, fast, and tested by every browser vendor. Hand-rolling JSON escaping is how you get parse errors with surrogate pairs and U+2028 (line separator, which used to break JSONP).

HTML escaping — the OWASP rule of five

OWASP's HTML escaping recommendation is to encode five characters in element bodies:

CharEntityWhy
&&amp;Must come first or it'll re-escape your other entities
<&lt;Prevents tag injection
>&gt;Defensive; some legacy parsers care
"&quot;Prevents attribute-value breakout
'&#39; or &#x27;Same — &apos; isn't valid in HTML4

This baseline covers element-body and double-quoted-attribute contexts. It does not cover JavaScript-event attributes (onclick), style attributes, or href/src URLs — those need additional context-specific encoding. The rule of thumb: never put untrusted data inside <script>, <style>, or unquoted attributes, ever.

URL encoding — encodeURIComponent vs encodeURI

This trips up everyone at least once. JavaScript ships two URL-encoding functions and they do different things:

If you ever find yourself reaching for escape() — stop. escape() is deprecated, produces non-standard output for non-ASCII, and is the wrong answer for everything in 2026.

CSV escaping — the RFC 4180 rules

CSV escaping is delightfully short:

  1. If the field contains ,, ", \r, or \n, wrap the entire field in double quotes.
  2. Inside that wrapping, double up any embedded " (so a single quote becomes "").

That is it. There is no standard backslash escaping in CSV; \n as two characters is treated literally. The other major risk — CSV injection — is when a field starts with =, +, -, or @ and Excel evaluates it as a formula on import. The OWASP fix: prefix any such cell with a single quote (') before saving. The picker handles RFC 4180 escaping; CSV-injection prefixing is a downstream sanitization step.

SQL escaping — why you should not be doing it

SQL "escaping" is the most dangerous category in this list. The historical pattern was to double-up single quotes (O'BrienO''Brien) and string-concatenate the result into a query. Do not do this. Quoting alone does not defend against:

The correct answer is parameterised queries / prepared statements, where the database driver sends the SQL template and the values separately and the database engine never confuses them. Every modern driver supports this:

Node.js (pg):
await client.query('SELECT * FROM users WHERE email = $1', [userEmail]);
Python (psycopg / sqlalchemy):
cur.execute('SELECT * FROM users WHERE email = %s', (user_email,))
Java (JDBC):
PreparedStatement ps = conn.prepareStatement(
  "SELECT * FROM users WHERE email = ?");
ps.setString(1, userEmail);

The SQL escape mode in this tool exists for emergency cases (one-off scripts, escaping a literal for a SQL editor pasted by hand). It should never be the layer that protects production code from injection.

JavaScript string escaping — the surprises

JavaScript string literals support a long list of escapes, and a few have non-obvious behavior:

JavaScript also has two characters that can break inline JSON-in-script blocks: U+2028 (line separator) and U+2029 (paragraph separator). They are valid in JSON but were illegal in JS string literals until 2019. Older code that writes <script>var data = ${JSON.stringify(x)};</script> needs to escape these to / for safety.

Common escaping mistakes that turn into vulnerabilities

Frequently Asked Questions

What is string escaping and why is it needed?
String escaping is the process of replacing special characters with escape sequences so they can be safely included in strings, code, or data formats. For example, a double quote inside a JSON string must be written as \" to avoid prematurely ending the string. Without proper escaping, special characters like quotes, backslashes, newlines, and tabs would break parsers, cause syntax errors, or create security vulnerabilities such as SQL injection and XSS attacks.
How do I escape special characters in a JSON string?
In JSON, the following characters must be escaped inside strings: double quotes (\"), backslash (\\), newline (\n), tab (\t), carriage return (\r), form feed (\f), backspace (\b), and any Unicode control character using \uXXXX notation. JavaScript's JSON.stringify() handles this automatically. This tool performs the same escaping entirely in your browser.
What is the difference between HTML encoding and URL encoding?
HTML encoding converts characters to HTML entities (e.g., < becomes &lt;) so they display as visible text in web pages instead of being interpreted as markup. URL encoding (percent-encoding) converts characters to %XX format (e.g., a space becomes %20) so they can be safely included in URLs. They serve different purposes and are not interchangeable — HTML encoding prevents markup injection in pages, while URL encoding ensures characters are transmitted correctly in URLs.
How do I unescape a backslash-escaped string?
To unescape a backslash-escaped string, replace each escape sequence with its corresponding character: \n with a newline, \t with a tab, \" with a double quote, \\ with a single backslash, and \uXXXX with the corresponding Unicode character. This tool handles all standard escape sequences automatically. In JavaScript, JSON.parse() can unescape JSON-style escaped strings when the input is wrapped in double quotes.
Why is string escaping important for security?
Proper string escaping prevents injection attacks — the most common class of web security vulnerabilities. Without escaping, attackers can inject malicious code: SQL injection exploits unescaped quotes in database queries, XSS (Cross-Site Scripting) exploits unescaped HTML and JavaScript in web pages, and command injection exploits unescaped shell characters. Always escape user input before inserting it into SQL queries, HTML output, URLs, or shell commands.

Browse all 50 free developer tools

All tools run in your browser, no signup required, nothing sent to a server.