I need to encode a whole text while leaving the < and > intact.
example
<p>Give me 100.000 €!</p>
must become:
<p>Give me 100.000 €!</p>
the html tags must remain intact
Use a regular expression that matches either a tag or what's between tags, and encode what's between:
html = Regex.Replace(
html,
"(<[^>]+>|[^<]+)",
m => m.Value.StartsWith("<") ? m.Value : HttpUtility.HtmlEncode(m.Value)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With