How String Escaper works
This tool escapes a string for seven targets: a JSON string, a JavaScript string literal, a SQL single-quoted string, a CSV field, a shell argument, a regular expression literal and an HTML attribute value. Each one uses a different rule. JSON and JavaScript put a backslash in front of quotes and backslashes and turn line breaks into \n. SQL and CSV double the quote character instead. A shell argument is wrapped in single quotes, a regex gets a backslash before every metacharacter, and an HTML attribute uses entities such as ". Set Direction to Unescape to reverse the change. The text is processed in your browser and is not sent anywhere.
What each format escapes
These are the exact changes this tool makes for each choice in Escape for.
| Format | Characters changed | Written as | Wrapper when quotes are included |
|---|---|---|---|
| JSON string, JavaScript string literal | Double quote, backslash, line break, carriage return, tab, backspace, form feed, other control characters | \" \\ \n \r \t \b \f, and \u followed by four hex digits for the rest | Double quotes |
| SQL single-quoted string | Single quote | Two single quotes | Single quotes |
| CSV field | Double quote | Two double quotes | Double quotes, always added when the field has a comma, quote or line break |
| Shell argument | Single quote | '\'' | Single quotes, always added |
| Regular expression literal | . * + ? ^ $ { } ( ) | [ ] \ / | The same character with a backslash in front | None |
| HTML attribute value | & " ' < > | & " ' < > | Double quotes |
Accented letters, emoji and other non-ASCII characters are left as they are in every format.
The same string escaped seven ways
Input: O'Brien, with Include surrounding quotes ticked.
| Escape for | Result |
|---|---|
| JSON string | "O'Brien" |
| JavaScript string literal | "O'Brien" |
| SQL single-quoted string | 'O''Brien' |
| CSV field | "O'Brien" |
| Shell argument | 'O'\''Brien' |
| Regular expression literal | O'Brien |
| HTML attribute value | "O'Brien" |
An apostrophe is only a problem where the single quote is the delimiter (SQL, shell) or where both quote types are escaped for safety (HTML attributes).
String escape examples
Each result is the tool's output with Include surrounding quotes ticked.
Multi-line text into JSON
She said "hi" [line break] New line [tab] Tabbed C:\temp
"She said \"hi\"\nNew line\tTabbed C:\\temp"
The line break and tab become the two-character sequences \n and \t, so the value fits on one line.
A URL into a regex
https://example.com/a?b=1
https:\/\/example\.com\/a\?b=1
Without the escapes, the dot would match any character and the question mark would make the a optional.
Text with markup characters into an HTML attribute
a "b" & <c>
"a "b" & <c>"
Unescaping a JSON string
"Line one\nLine \"two\"\t\u00e9" with Direction set to Unescape
Line one, then on a new line: Line "two", a tab, and the letter é
The outer quotes are removed and every escape sequence is turned back into the character it stands for.
Unescaping a SQL literal
'O''Brien' with Direction set to Unescape
O'Brien
Escaping SQL by hand is not a defence against injection
The SQL option exists for writing a one-off statement yourself, for example an INSERT you are about to paste into a database console. It doubles single quotes and does nothing else.
That is not enough for application code. Some databases, MySQL in its default mode among them, also treat a backslash as an escape character, so a rule that is correct for one server is incomplete on another. Hand escaping also has to be applied at every place a value enters a query, and one missed place undoes the rest.
Parameterised queries avoid the problem: the statement and the values are sent separately, so a value is never parsed as SQL. The same idea applies to the other formats. Build JSON with a serialiser, write CSV with a CSV library, and pass arguments to a subprocess as a list instead of assembling a shell command as one string.
Limits of the Unescape direction
Unescape reverses what Escape produces. It is not a full parser for each language.
- JSON and JavaScript share one unescaper that follows JSON rules. JavaScript-only sequences such as \' and \x41 are rejected with the message that the text does not look like a valid escaped string.
- The JSON and JavaScript unescaper also rejects input that contains a real line break or tab. An escaped string keeps those as \n and \t, so paste it as a single line.
- HTML attribute value turns ", ' and & back into characters, but leaves < and > as they are. Use the HTML entity encoder to decode every entity.
- SQL, CSV and Shell argument remove one quote from each end, then undo the doubled quote or the '\'' sequence.
- Regular expression literal removes the backslash only from the characters the escaper adds one to, so classes such as \d and \w are left untouched.
How to use it
- 1
Choose the destination in Escape for: JSON string, JavaScript string literal, SQL single-quoted string, CSV field, Shell argument, Regular expression literal or HTML attribute value.
- 2
Leave Direction on Escape, or switch it to Unescape to turn an escaped string back into plain text.
- 3
Tick or untick Include surrounding quotes depending on whether you want the delimiters in the output.
- 4
Paste your text into Input, line breaks and tabs included.
- 5
Copy the Escaped result and read the note under it, which describes the rule used for that format. When unescaping, the result is labelled Unescaped and has no note.