String Escape / Unescape

Escape or unescape text for JavaScript, JSON, SQL, shell or CSV.

Examples

Questions

Are JavaScript and JSON escaping the same?

Here, yes. Every string JSON.stringify makes is also a valid JavaScript string.

How does shell escaping work?

The whole string is wrapped in single quotes. Inside single quotes, a POSIX shell keeps every character as it is except the single quote itself, which becomes '\''.

What does CSV escaping do?

As RFC 4180 says: a field is quoted only if it has a comma, a double quote or a line break. A double quote inside is doubled.

Why does unescaping sometimes fail?

JSON and JavaScript fail on a broken escape, such as a stray quote or an incomplete \u escape. Shell expects single-quoted input. SQL and CSV never fail.