String Encoder/Decoder
About the String Encoder/Decoder
This string encoder escapes and unescapes text for four contexts: HTML entities, XML entities, JavaScript string literals and CSS identifiers. Pick the target syntax, paste your text and the escaped (or decoded) result appears instantly, so a quote or newline never breaks your markup or code again.
Why escaping depends on context
Each language treats a different set of characters as special. In HTML, < starts a tag; in a JavaScript string, an unescaped " ends the literal; in a CSS selector, a leading digit makes an ID invalid. Escaping replaces those characters with a form the parser reads as plain data.
Using the wrong scheme fails quietly. " inside a JavaScript string is just six literal characters, and \n inside HTML is shown as a backslash and an "n". Always match the escape to the place the string will end up.
HTML vs XML entities
Both modes encode the same five characters: & to &, < to <, > to >, " to " and the apostrophe. The difference is the apostrophe: HTML mode writes ', which every browser understands, while XML mode writes ', one of XML's five predefined entities.
Decoding HTML understands every HTML5 named entity such as © or ♥, plus decimal (©) and hex (©) references. XML decoding resolves the predefined entities and numeric references and leaves unknown names untouched, as an XML parser would reject them.
JavaScript string escapes explained
Encoding to a JavaScript string escapes backslashes, both quote types and control characters: a line break becomes \n, a tab \t, and other control characters become \u escapes such as \u001b. The line and paragraph separators U+2028 and U+2029 are escaped too, since older engines treat them as line breaks inside string literals.
Decoding accepts \uXXXX, ES2015 code point escapes like \u{1F600}, \xXX hex escapes and the single-letter escapes, so you can paste a string straight from source code or a JSON log.
CSS identifier escaping
CSS mode uses the browser's own CSS.escape(), the same function you would call before building a selector in JavaScript. An ID like 1st-item becomes \31 st-item, and a:b becomes a\:b, so document.querySelector will accept it.
This is for identifiers (class names, IDs, custom property names), not for escaping arbitrary text inside a content: "..." string value.
How to Use the String Encoder/Decoder
- Under Operation Mode, choose Encode to escape text or Decode to turn escapes back into characters.
- Under Target Syntax, pick HTML Entities, XML Entities, JavaScript String or CSS Escape (identifier).
- Type or paste your text into Source Text. The result updates on every keystroke.
- If decoding fails, for example on an invalid code point such as �, the error is shown above the panels.
- Use the copy icon on the result panel to copy it, or the trash icon to clear the input.
Frequently Asked Questions
- Is my text sent anywhere?
- No. Escaping and unescaping run locally in your browser with plain JavaScript and built-in browser APIs.
- Does HTML encoding prevent XSS?
- Encoding the five special characters is the right defense for text placed inside HTML elements and quoted attributes. It is not enough inside <script> blocks, href URLs or event handlers, which need their own escaping or validation.
- Why does the apostrophe become ' instead of '?
- ' was not defined in HTML 4, so ' is the safer choice for HTML. XML mode uses ', which XML has always defined.
- Can I use JavaScript escaping for JSON?
- Mostly, but JSON only allows double-quoted strings and does not accept \', \v, \0 or \x escapes. For JSON, run your value through JSON.stringify or a JSON formatter.
- Why does CSS mode escape a leading digit as \31 ?
- CSS identifiers cannot start with a digit, so the digit is written as its hex code point followed by a space. \31 is the digit 1.
- What is the difference between this and the HTML Encoder tool?
- This tool covers four syntaxes with minimal escaping. The HTML Encoder focuses on HTML only and adds options like encoding all non-ASCII characters and converting line breaks to <br>.