Utilia

String Escape/Unescape

Escape or unescape a string for JSON, JavaScript, template literals, SQL, CSV, HTML attributes, regular expressions and URLs, right in your browser.

How to escape or unescape a string

  1. Pick the target in Format: JSON string, JavaScript string, JavaScript template literal, SQL string literal, CSV field, HTML attribute value, regular expression or URL component.
  2. Type or paste your text in the input box.
  3. Click Escape to turn plain text into a safe literal, or Unescape to turn a literal back into plain text. The result updates as you type, in the direction you last chose.
  4. Adjust the options shown for the format: Add surrounding quotes, Escape non-ASCII characters or Spaces as + (form data).
  5. Click Copy, or Use output as input to reverse the operation and check the round trip.

The character counts before and after are shown under the buttons. Everything runs in your browser; nothing is uploaded.

What each format does

  • JSON string: escapes ", \ and control characters (\n, \t, \u0000…), exactly like JSON.stringify. With Escape non-ASCII characters, every character above U+007E becomes \uXXXX (emoji become a surrogate pair such as \ud83d\ude00). Unescaping accepts the text with or without its double quotes, and tolerates raw line breaks.
  • JavaScript string (‘…’): escapes the backslash, the single quote, line breaks and control characters, plus U+2028 and U+2029, which break older engines. Unescaping understands \x41, \u0041, \u{1F600}, legacy octal escapes such as \101 and line continuations.
  • JavaScript template literal (`…`): escapes backticks, backslashes and ${ so no expression is evaluated; line breaks and tabs stay as they are, since template literals allow them.
  • SQL string literal (‘…’): doubles single quotes (O'Brien → 'O''Brien'), as in standard SQL. Unescaping refuses a quote that is not doubled. Backslashes are left alone: MySQL in its default mode also treats \ as an escape character, so check your database’s rules.
  • CSV field (RFC 4180): wraps the field in double quotes and doubles inner quotes when it contains a comma, semicolon, tab, quote, line break or leading/trailing space. Add surrounding quotes forces the quotes.
  • HTML attribute value: replaces &, <, >, " and ' with entities, which is safe in both single- and double-quoted attributes. Unescaping decodes numeric references and &amp;, &lt;, &gt;, &quot;, &apos; and &nbsp;.
  • Regular expression (literal text): backslash-escapes . * + ? ^ $ { } ( ) | [ ] \ /, plus line breaks and tabs, so the pattern matches the text literally. The result is valid with or without the u flag.
  • URL component (percent-encoding): works like encodeURIComponent, encoding everything except letters, digits and - _ . ! ~ * ' ( ) as UTF-8 bytes. Spaces as + (form data) uses the application/x-www-form-urlencoded convention.

Escaping is not a security boundary for SQL

When you choose SQL, the tool shows a reminder, and it deserves repeating: the real fix for SQL injection is a parameterized query (prepared statement), where the value is sent separately from the SQL text. Escaping by hand is fragile: it depends on the database, the connection character set and settings such as MySQL’s backslash handling. Use this output to read logs, write fixtures or debug a query, never to build queries from user input.

For CSV, the tool also warns when a value starts with =, +, - or @: a spreadsheet may run it as a formula (CSV injection). Quoting does not prevent that; prefixing the value with an apostrophe or a space is the usual mitigation.

Frequently asked questions

What is the difference between a JSON string and a JavaScript string?

JSON only allows double quotes and a small set of escapes (\" \\ \/ \b \f \n \r \t \uXXXX). JavaScript also accepts single quotes, backticks, \x41, \u{…}, \v and \0. Valid JSON escapes are valid JavaScript, but not the other way round.

Why does unescaping fail with "invalid escape sequence"?

The text contains a backslash followed by something the format does not allow, such as \u12 with fewer than four hex digits, or a trailing single backslash. The error message shows the faulty sequence.

Does CSV need quotes around every field?

No. RFC 4180 only requires quotes when the field contains a comma, a double quote or a line break. This tool also quotes semicolons, tabs and leading or trailing spaces, because many spreadsheets use them as delimiters or trim them.

Is the HTML attribute escape enough to prevent XSS?

It is correct for text placed inside a quoted attribute value. It is not enough for attributes that are themselves code or URLs, such as onclick or href="javascript:…"; those need validation, not just escaping.

Is my text sent to a server?

No. All conversions run locally in your browser, so you can safely paste configuration values or data you would not share.