Toolvist

String Escape / Stringify

Escape quotes, backslashes and special characters to produce a safe JavaScript or JSON string literal. Free online string escaper, instant results.

Free Online String Escape Tool

The String Escape tool escapes quotes, backslashes, and special characters so text can be safely embedded in JavaScript, JSON, or C# source. It can also reverse the process to unescape a string.

The decoded form is read with the Unicode to Text Converter, and the other escape conventions are covered by the URL Encoder & Decoder and HTML Entity Encoder & Decoder. For the encodings rather than escapes, the Base64 to Hex Converter is the sibling.

How to use the String Stringify

  1. 1 Paste your text.
  2. 2 Choose the target language or format.
  3. 3 Convert. The result is the same text with the correct quoting and escaping for that context.
  4. 4 Copy the escaped string.

Escaping in the right context

Escaping is context-dependent, and the context is determined by where the string will be interpreted. A value destined for a SQL query needs SQL escaping; a value destined for HTML needs HTML escaping; a value embedded in JSON needs JSON escaping. The same input produces three different correct outputs.

The mistake that causes vulnerabilities is escaping for one context and then inserting into another. Text escaped for HTML and placed inside a JavaScript string literal is not safe, because the HTML escaping does not address the JavaScript parser's rules. Each boundary needs its own treatment.

  • Match the escaping to the language that will parse the string.
  • Backslashes, quotes and control characters are the usual targets.
  • HTML escaping and string escaping are different operations.
  • Escape at the boundary, right before the value is used.

Frequently asked questions

What does escaping actually protect against?

Ambiguity. A string that contains the quote character used to delimit it is syntactically broken unless the inner character is escaped. Similarly, a string containing a backslash, a newline or a control character has to be represented in a way the parser can read back unambiguously.

Why does the output differ between languages?

Because each language has its own escape vocabulary. JSON and JavaScript share most of theirs, but a language with different string delimiters or different support for interpolation needs a different approach. Converting across languages means matching the target's rules, not applying one universal escaping.

Should I escape for HTML the same way?

No, and this is a common and dangerous confusion. HTML escaping prevents markup injection; string escaping prevents syntax errors. Applying one where the other is needed produces code that either breaks or, worse, appears to work while leaving an injection point open.

Related Developer Tools

Browse all Developer Tools →