Xerobit

Escape Special Characters in Any Language

Use this tool to escape special characters in a string or unescape string literals for JavaScript, JSON, Python, Java, C, SQL, regex and shell. Select your target language and direction, paste your text, and get the correctly escaped output instantly. Everything runs in your browser so nothing is uploaded to any server.

Result

How to use the string escape and unescape

  1. Paste or type the text you want to process into the Input string field.
  2. Select the target Language from the dropdown, such as JavaScript, Python, SQL or Shell.
  3. Choose whether to Escape raw text into a safe literal, or Unescape an existing escaped string back to raw text.
  4. The result updates live below the input. Copy it directly into your code.

How escaping works across languages

When you escape special characters, you replace control characters, quotes and other reserved symbols with a sequence that a parser can read safely. For example, a newline character becomes \n, a tab becomes \t, and a double quote becomes \". The exact rules depend on the language.

For JavaScript the output is the body of a double-quoted string and is also valid JSON: backslashes, double quotes, newlines, carriage returns, tabs, backspaces and form feeds become their two-character sequences, other control characters and every character above U+007E become \uXXXX, and emoji become surrogate pairs. Single quotes are left alone, so wrap the result in double quotes. Unescape reverses these sequences back into the original characters.

Common escape sequences reference

Different languages share many escape sequences but diverge on others. Here is a reference table of how common characters are escaped in several targets:

CharacterJavaScript / JSONPythonC / JavaSQL
Newline\n\n\nN/A (use CHAR)
Tab\t\t\tN/A
Backslash\\\\\\\ (varies)
Double quote\"unchanged\"" (varies)
Single quoteunchanged\'\'''
Carriage return\r\r\rN/A

For SQL, single quotes are typically escaped by doubling them (''). If you need dedicated database formatting, see the SQL formatter or the specific sql escape tool.

Escaping for Python vs JavaScript

Developers frequently search for ways to escape string Python code or handle a python escape string because Python offers multiple string literal types. Standard strings use backslash escapes just like JavaScript, but Python also supports raw strings (prefixed with r) where backslashes are treated as literal characters.

When you escape a JavaScript string or perform a js escape string operation, the rules are stricter. JSON, which shares JavaScript string syntax, requires double quotes and does not allow literal control characters. A string escape operation for JSON must convert all newlines and tabs to their \n and \t representations. The JavaScript output here matches Python's json.dumps(text) (with its default ASCII-only output) and parses back with JSON.parse. The Python output is meant for a single-quoted literal, so it escapes ' and leaves double quotes alone.

Regex and Shell escaping

Regular expressions and shell scripts have completely different sets of special characters. In regex, characters like ., *, +, ?, ^, $, {, }, [, ], (, ), |, and \ carry structural meaning. To match them literally, you must prepend a backslash. You can test your patterns using the regex tester or generate them with the regex generator.

Shell escaping is about preventing the terminal from interpreting spaces, quotes, dollar signs, ampersands and other metacharacters. The safest approach for Bash is often to wrap the entire string in single quotes, replacing any internal single quotes with '\''. Alternatively, prepending a backslash to every special character works for short strings.

Handling Unicode and control characters

Beyond standard ASCII, strings often contain Unicode characters or invisible control codes. When you escape special characters that fall outside the printable ASCII range, this tool uses \uXXXX notation for JavaScript, Java and Python, with \UXXXXXXXX for supplementary characters in Python. C and C++ output is written as the UTF-8 bytes in three-digit octal escapes such as \303\251, which cannot swallow a following digit the way a hex escape can. If you specifically need to convert text to and from Unicode code points, the unicode escape converter is built for that purpose.

Control characters like the null byte, backspace (\b) and form feed (\f) are also handled: they become \u0000, \b and \f in JavaScript. These characters can break parsers or cause security vulnerabilities if left unescaped in database queries or command-line arguments. Always verify your output length with a character counter if strict limits apply.

Worked examples

ExampleInputResult
Escape a JS string with newlinesInput string: Hello / World "quoted"; Language: javascript; Direction: escapeHello\nWorld\t\"quoted\"
Unescape a Python stringInput string: Line 1\nLine 2\tTabbed; Language: python; Direction: unescapeLine 1 / Line 2 Tabbed
Escape for SQLInput string: O'Reilly's "book"; Language: sql; Direction: escapeO''Reilly''s "book"
Escape regex metacharactersInput string: file.txt (copy); Language: regex; Direction: escapefile\.txt \(copy\)

FAQ

What does it mean to escape special characters?

Escaping means replacing characters that have a special meaning in a programming language with a safe sequence, usually starting with a backslash. For example, a newline becomes \n so the parser reads it as part of the string rather than an actual line break.

How do I escape JavaScript string values?

To escape JavaScript string content for a double-quoted string or JSON, replace backslashes with \\, double quotes with \", newlines with \n, tabs with \t and carriage returns with \r, and write other control and non-ASCII characters as \uXXXX. This tool automates that and the result also works as JSON string content.

How do I unescape string data back to normal text?

Unescaping reverses the process. Sequences like \n are converted back into actual newline characters, and \\ becomes a single backslash. Select 'Unescape' and choose the correct language to decode the literal properly.

How do I escape a Python string compared to JavaScript?

Python and JavaScript share most basic escape sequences like \n, \t and \\. However, Python also supports raw strings where backslashes are ignored, uses \xNN for control characters and \UXXXXXXXX for characters above U+FFFF. This tool produces text for a single-quoted Python literal; unescape reads \ooo, \xNN, \uXXXX and \UXXXXXXXX.

How are single quotes escaped in SQL?

In standard SQL, single quotes inside a string are escaped by doubling them. The word O'Reilly becomes O''Reilly. Backslashes are not used for quote escaping in standard SQL, though some dialects support them.

Does this tool work for HTML or XML?

This tool focuses on programming languages, regex and shell. If you need to escape character HTML entities or handle XML, use the dedicated HTML escape or XML escape tools instead.

Is my data sent to a server?

No. The string escape and unescape logic runs entirely in your browser. Your text is never uploaded anywhere.