String Escape & Unescape: how it works and how to build one
Escape text for JSON, JavaScript, regex, SQL or Unicode. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Escapes quotes, backslashes, new lines and special characters so text can go inside a JSON or JavaScript string, a regular expression or a SQL literal. Unescape turns \n, \t and \u00e9 sequences back into real characters.
When to use it
- Putting text inside JSON or code
- Building a regex from user input
- Reading logs full of \n and \u sequences
How to use it
- Pick the style.
- Choose Escape or Unescape.
- Paste text and copy the result.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/string-escape
How it works
Each language has its own escape rules. For JSON and JavaScript, JSON.stringify already escapes quotes, backslashes and control characters, so the tool uses it and drops the outer quotes. For regex, every special character gets a backslash. For SQL, a single quote is doubled.
Unescaping uses one regex that recognises \n, \t, \uXXXX, \u{…} and \xXX sequences and replaces each with the real character.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
escape.ts (ts)
export const escapeJson = (s: string) => JSON.stringify(s).slice(1, -1);
export const escapeRegex = (s: string) => s.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
export const escapeSql = (s: string) => s.replace(/'/g, "''");
export const unescape = (s: string) =>
s.replace(/\\u\{([0-9a-f]+)\}|\\u([0-9a-f]{4})|\\x([0-9a-f]{2})|\\(.)/gi, (_, cp, u, x, c) =>
cp || u || x ? String.fromCodePoint(parseInt(cp || u || x, 16))
: ({ n: "\n", t: "\t", r: "\r", "0": "\0" } as Record<string, string>)[c] ?? c);
Make it production-ready
- Add HTML and CSV escaping modes.
- Show invisible characters so users see what was escaped.
Questions
What does Unicode escape do?
It writes every non-ASCII character as \u followed by its code, so the text is safe in ASCII-only files.
Free, runs in your browser, nothing is uploaded.