Skip to content

unescape_html

Decodes the HTML entities &, <, >, ", ', ' and any numeric character reference (A, A).

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 &.

use helpers4::string::unescape_html;

Cargo feature string (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features string

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.5", default-features = false, features = ["string"] }
pub fn unescape_html(s: &str) -> Cow<'_, str>
ParameterTypeDescription
s&strThe text to decode.

Cow<'_, str>

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;");

src/string/unescape_html.rs