I’m looking for a way to look for a specific string within a page and then wrap that string in <em>
tags. I have tried used HTML Agility Pack and had some success with a Regex.Replace
but if the string is included within a url it also gets replaced, if it’s within an image name, it gets replaced and this obviously breaks the link or image url.
An example:
var markup = Encoding.UTF8.GetString(buffer);
var replaced = Regex.Replace(markup, "product-xs", " <em>product</em>-xs", RegexOptions.IgnoreCase);
var output = Encoding.UTF8.GetBytes(replaced);
_stream.Write(output, 0, output.Length);
This would replace a <a href="product/product-xs">
with <a href="product/<em>product</em>-xe">
The string is coming from a text string value within a CMS so the user can’t wrap the words there and ideally, I want to catch all instances of the word that are already published.
Ideally I would want to exclude <title>
tags, <img>
tags and <a href>
and <a>
tags, everything else should get the wrapped tag.
Before I used the HTML Agility Pack, a fellow front end dev tried it with javascript but that had an unexpected impact on dropdown menus.
If you need any more info, just ask.