String Escape / Unescape (Backslash)

Most languages use a backslash to start an escape, but the characters after it mean different things in each one. Pick the target language, paste the text, and get a string literal you can drop into source code, or paste a literal and read what it contains. The conversion runs in JavaScript in this page, so nothing is sent to a server.

Plain Text
String Literal

1 String Escape / Unescape (Backslash) example

Plain text

It's "$5.00" for O'Brien
C:\Users\zoe\notes.txt
Tab:	done ✓

The same text as a literal in five languages

// JavaScript
"It\'s \"$5.00\" for O\'Brien\nC:\\Users\\zoe\\notes.txt\nTab:\tdone ✓"

// Python
"It\'s \"$5.00\" for O\'Brien\nC:\\Users\\zoe\\notes.txt\nTab:\tdone ✓"

// PHP
"It's \"\$5.00\" for O'Brien\nC:\\Users\\zoe\\notes.txt\nTab:\tdone ✓"

// Bash $'...'
$'It\'s "$5.00" for O\'Brien\nC:\\Users\\zoe\\notes.txt\nTab:\tdone ✓'

// SQL
'It''s "$5.00" for O''Brien
C:\Users\zoe\notes.txt
Tab:	done ✓'

2 Where the languages differ

Unknown escapes

JavaScript silently drops the backslash from \q and keeps the q. Python and PHP keep both characters (Python 3.12 also warns). Java refuses to compile. The unescape mode follows the selected language and lists any unknown sequences it met.

Bytes or characters

In JavaScript and Python, \xE9 is the character é. In PHP and Bash it is a single byte 0xE9, which is not valid UTF-8 on its own. For é in PHP you want \xC3\xA9 or \u{E9}. The tool decodes PHP and Bash escapes as bytes and tells you when they do not form valid UTF-8.

Code points above U+FFFF

JavaScript and Java escapes are UTF-16, so 😀 becomes the pair \uD83D\uDE00. Python writes \U0001f600, PHP \u{1F600}, Bash \U0001F600. Unescape joins surrogate pairs back into one character.

SQL is not backslash

Standard SQL escapes a quote by doubling it: 'O''Brien'. MySQL also treats backslash as an escape unless NO_BACKSLASH_ESCAPES is on; PostgreSQL only does inside E'...'. This mode is for reading and writing literals by hand. In application code, use bound parameters.

3 The two Bash modes

Bash single quotes is the mode to use for passing an argument safely. Inside single quotes nothing is special, so the only character that needs work is the single quote itself, which becomes '\'': close the quote, add an escaped quote, reopen. Line breaks, dollar signs and backslashes all survive untouched. This is what shlex.quote() and PHP's escapeshellarg() do.

Bash $'...' is ANSI-C quoting, which reads backslash escapes like C does. It is the readable choice when the string holds tabs or other control characters. \u and \U need Bash 4.2 or newer, which rules out the Bash 3.2 that macOS still ships, so the non-ASCII option is off by default. The unescape side understands a full shell word, so 'it'"'"'s' and 'it'\''s' both come back as it's.

Regex escapes the metacharacters \ ^ $ . * + ? ( ) [ ] { } | / so the text matches as plain characters. It leaves the hyphen alone because escaping it outside a character class is an error with the u flag. Test the result in the regex tester.

How to use the String Escape / Unescape (Backslash) tool

  1. Choose Escape or Unescape in the Mode dropdown, then the target in Language.
  2. Paste text into the left panel. The escaped literal (or decoded text) appears on the right as you type.
  3. When escaping, Add quotes wraps the result in the language's quote style, and Escape non-ASCII turns accents and emoji into \u, \x or \U escapes.
  4. When unescaping, paste the literal with or without its quotes. Errors show a line and column; the note bar lists escapes the language would keep as-is.
  5. Swap flips the direction with the output as the new input, and Copy Result copies it.

How to escape strings in JavaScript, PHP and Python

JavaScript

const text = `It's "$5"\n\tC:\\dir`;

// A double-quoted JavaScript literal: JSON.stringify output is valid JS
console.log(JSON.stringify(text)); // "It's \"$5\"\n\tC:\\dir"

// Escape user input before putting it in a RegExp
// (ES2025 adds RegExp.escape() for exactly this)
const escapeRegex = (s) => s.replace(/[\\^$.*+?()[\]{}|/]/g, "\\$&");
const re = new RegExp("^" + escapeRegex("price (USD) $5.00") + "$");
console.log(re.test("price (USD) $5.00")); // true

// Unescape without eval(): JSON.parse understands the escapes
// JSON shares with JavaScript (not \x41, \' or octal)
console.log(JSON.parse('"line\\nbreak \\u00e9"'));

PHP

<?php
$text = "It's \"\$5\"\n\tC:\\dir";

// A PHP literal you can paste back into code
echo var_export($text, true), PHP_EOL;

// Shell argument: never build it by hand
echo 'grep ' . escapeshellarg("it's") . ' notes.txt', PHP_EOL; // grep 'it'\''s' notes.txt

// Regex: preg_quote, and pass your delimiter
echo preg_quote('price (USD) $5.00 / unit', '/'), PHP_EOL;

// Decode C-style escapes (\n, \t, \xHH, octal) in a string
echo stripcslashes('tab\there \x41\102'), PHP_EOL; // tab<TAB>here AB

// SQL: bound parameters, not escaping
// $stmt = $pdo->prepare('SELECT * FROM users WHERE name = ?');
// $stmt->execute(["O'Brien"]);

Use escapeshellarg() for shell arguments and preg_quote() for regexes. addslashes() is not an SQL escape.

Python

import re
import shlex

text = 'It\'s "$5"\n\tC:\\dir'

print(repr(text))                         # a valid Python literal
print(shlex.quote("it's"))                # 'it'"'"'s'
print(re.escape("price (USD) $5.00"))     # price\ \(USD\)\ \$5\.00

# Decode backslash escapes. unicode_escape works on bytes and reads
# anything that is not an escape as Latin-1, so feed it ASCII only.
escaped = r"tab\there \u00e9 \x41"
print(escaped.encode("ascii").decode("unicode_escape"))  # tab<TAB>here é A

shlex.quote() is for POSIX shells. It is not safe for cmd.exe or PowerShell.

Common errors and how to fix them

SyntaxError: Invalid hexadecimal escape sequence
JavaScript's \x needs exactly two hex digits and \u exactly four (or \u{...}). A Windows path like C:\xampp in a normal string literal triggers it; double the backslashes or use String.raw.
SyntaxWarning: invalid escape sequence '\d'
Python 3.12+ warns when a normal string has a backslash that is not an escape, typically in a regex. Use a raw string, r"\d+", so the backslash reaches the regex engine.
PHP prints \n literally instead of a line break
The string is in single quotes. PHP processes \n, \t, \x and the other escapes only inside double quotes and heredoc; single quotes understand only \' and \\.
Bash prints \u00e9 instead of é
The $'...' \u and \U escapes need Bash 4.2 or newer, and macOS still ships 3.2 as /bin/bash. Use printf with the UTF-8 bytes (\xc3\xa9), or run a newer bash.

Good to know

Escaping follows the syntax of one specific literal. The same text needs different treatment as a JavaScript string, a shell argument, an SQL literal and a regex, and escaping for the wrong one is how injection bugs happen. Where a proper API exists (bound SQL parameters, argument arrays for subprocesses, textContent in the DOM), use it instead of escaping.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

How do I escape a single quote in a Bash string?

Inside single quotes nothing can be escaped, so close the quote, add an escaped quote and reopen: 'it'\''s'. Pick Bash single quotes in the Language dropdown and the tool writes it for you.

How do I escape a single quote in SQL?

Double it: 'O''Brien'. That is standard SQL and works in every major database. In application code, use bound parameters instead of escaping by hand.

Why does \xE9 give a broken character in PHP but é in JavaScript?

In JavaScript and Python \xE9 is the character U+00E9. In PHP and Bash it is a single raw byte, which is not valid UTF-8 by itself. Use \xC3\xA9 or PHP's \u{E9} for é.

Which characters need escaping in a regular expression?

The metacharacters \ ^ $ . * + ? ( ) [ ] { } | and, inside a /.../ literal, the slash. The Regex option escapes exactly those, so the text matches as plain characters.

What happens to escapes the language does not know, like \q?

It depends on the language, and the tool follows the one you picked: JavaScript drops the backslash, Python, PHP and Bash keep it, and Java refuses to compile. Unknown sequences are listed under the tool.

Other escaping tools

For JSON specifically, the JSON escape tool follows RFC 8259 exactly. Text headed into markup belongs in the HTML encoder, text headed into a URL in the URL encoder, and to see the raw bytes behind a string try hex to text.

More Encoders & Decoders

Browse all encoders & decoders

Copied to clipboard!