I found this article on stackoverflow
highlight words in html using regex & javascript - almost there
Using the article above, I am trying to highlight HTML text on the server using c#. The code is shown below:
string replacePattern = "$1<span style=\"background-color:yellow\">$2</span>";
string searchPattern = String.Format("(?<=^|>)(.*?)({0})(?=.*?<|$)", searchString.Trim());
content = Regex.Replace(content, searchPattern, replacePattern, RegexOptions.IgnoreCase);
The code seems to work great except when trying to highlight a word that is contained in an image source:
Search Keyword:
ABC
Search Text:
<div><img src="/site/folder/ABC.PNG" /><br />ABC</div>
The result will highlight both the text and the image name.
Any help would be greatly appreciated.
spanaround a blockdiv. HTML is not a regular language, and irregular things will happen eventually. Make it more robust and parse it. However, depending on how you render your templates, maybe you can highlight while rendering the HTML..rather than after-the-fact.