piapia123
en

HTML and JS String Converter

Web

Everything runs locally in your browser — nothing is uploaded

Input
Drop a file here to load its contents
Output

One direction puts HTML into JS as a string literal: newlines, quotes and backslashes escaped, optionally wrapped as `const html = …` or `document.write(…)`. The other direction turns an escaped string copied out of code back into readable HTML. The dangerous case is `</script>`: if that JS lives inside a `<script>` tag, a `</script>` inside the string closes the tag early — the rest becomes visible text on the page, and the console may stay silent. So it is broken into `<\/` by default, and inside a JS string those two are the same value, so nothing changes.

Features

  • Both directions: HTML → JS string and JS string → HTML
  • Direction detected from the input (`<` means HTML, a quote means a JS string), or set it yourself
  • Breaks `</` into `<\/` by default, preventing a premature `</script>`
  • Three quote styles: single, double and backtick (backtick also escapes `${` and backticks)
  • Three output shapes: the bare string, `const html = …`, or `document.write(…)`
  • Escapes newlines, tabs, backslashes and the line separators U+2028 / U+2029
  • Unescaping understands `\n` `\t` `\xHH` `\uHHHH` `\u{…}` and line continuations
  • Unknown escapes keep their backslash — you may have pasted a regex

How to use

  1. Paste HTML, or a quoted JS string
  2. Leave Direction on Detect, or choose one
  3. Pick the quote style and output shape; the result updates live
  4. Copy the result — it is ready to paste into code

FAQ

Why turn </ into <\/?
Because inside a `<script>` tag the HTML parser only looks for the character sequence `</script` in the source — it does not care whether it sits inside a JS string. So any embedded HTML containing `</script>` closes the tag right there: the remaining JS becomes text on the page and the error message points somewhere unrelated. In a JS string `\/` and `/` are the same value, so breaking it changes no output — the only way to alter the source without altering the value.
Single quotes or double quotes?
It depends on the content. HTML attributes normally use double quotes (`class="btn"`), so putting HTML inside a single-quoted string needs far fewer escapes and reads better, which is why single is the default. Backticks (template literals) look cleanest, but cost extra escapes for `${` (otherwise interpolated) and for backticks themselves.
How is the direction detected?
By one test: does the input start with `<` once trimmed? That means it is markup. The reverse test — does this look like an escaped JS string — is far too weak, since almost any text can look like one, and guessing wrong means unescaping your prose as if it were code (a literal `\n` becomes a real newline) in a way that is invisible afterwards. So only one side is detected; pick the other by hand.
Why are some escapes left alone?
Unescaping only handles escapes with a defined meaning in JS: `\n` `\t` `\b` `\f` `\v` `\0`, `\xHH`, `\uHHHH`, `\u{…}`, and the quote and backslash characters themselves. Anything else (`\d`, `\q`) keeps its backslash rather than dropping it — you may have pasted a regex, and dropping the backslash turns `\d` into `d`, which means something else entirely.

Related tools