Skip to main content

unescape_html

Function unescape_html 

Source
pub fn unescape_html(s: &str) -> Cow<'_, str>
Expand description

Decodes the HTML entities &amp;, &lt;, &gt;, &quot;, &apos;, &#39; and any numeric character reference (&#65;, &#x41;).

It is the inverse of escape_html. The text is decoded in a single pass, so &amp;lt; becomes &lt; and not <. Anything that is not a known entity, including a numeric reference that is not a valid character, is left as it is. Returns the input borrowed, without allocating, when it contains no &.

§Arguments

  • s - The text to decode.

§Examples

use helpers4::string::unescape_html;

assert_eq!(unescape_html("&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;"), "<b>Tom & Jerry</b>");
assert_eq!(unescape_html("&#65;&#x42;"), "AB");
assert_eq!(unescape_html("&amp;lt;"), "&lt;");
assert_eq!(unescape_html("&unknown;"), "&unknown;");