Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a native Mathematica way to convert a string like "<select></select>" into "&lt;select&gt;&lt;/select&gt;" and similarly for other reserved characters in html (or another encoding that would work in a webpage)?.

I'm working in webMathematica, but I can't find a good way to print the literal string "<select></select>" into a page - it either gets converted into an node, or it gets wrapped in a <pre> tag.

share|improve this question
up vote 8 down vote accepted

The different HTML entities are stored in System`Convert`MLStringDataDump`$HTMLEntities on version 9 and from here, it's a simple StringReplace:

StringReplace["<select></select>", System`Convert`MLStringDataDump`$HTMLEntities]
(* "&lt;select&gt;&lt;/select&gt;" *)
share|improve this answer
The problem with this is that the necessary definitions are not present in a fresh kernel. We need to figure out what triggers auto-loading them. ExportString["asd", "HTML"] certainly does but maybe it's more than necessary. – Szabolcs Feb 19 '14 at 20:53
This is the line registering the exporter: ImportExport`RegisterExport[ "HTML", System`Convert`HTMLDump`exportUsingHTMLSave, "Sources" -> {"Convert`ConvertCommon`", "Convert`MLStringData`", "Convert`HTMLConvert`"}, "Unevaluated" -> False ] I'm not sure what the "Sources" option does exactly. – Szabolcs Feb 19 '14 at 20:59

Something like:

ExportString[Cell[TextData["<select></select>"],"Text"],"HTML","FullDocument" -> False]


<p class="Text">

which might also be a good start.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.