c# - Search and replace non-HTML content -
I have to write some code that will search and replace the entire word in a string outside the HTML tag. So if I have this string:
string content = "brown fox has jumped over lazy dogs" and ; String keyword = "the";
I need to do something like this:
if (content.ToLower (). It contains (keyword.ToLower ()) content = Content.Replace (keyword, string.format ("& lt; span style = \" background-color: yellow; \ "& gt; {0} , keyword));
But I do not want to change "the" in bold tags or "only", "the" before.
Try it out:
content = RegEx.Replace (content, "(? & Lt;; & Gt;)" + keyword + "(?! ( "+ keyword + '& lt; / Span & gt; ';
Edit: I have fixed the "this" case, but it is not where more than The keyword is wrapped in HTML, like, "Fox jumped on the lazy dog."
What are you asking, almost everyday HRM, RegEx and impossible with normal It's going to happen because to know if you're a In the tag, you have to "add" each start and end tag, and ignore tags that are intended for self-closing (for example BR and IMG).
If this There is only eye candy for the web site, I suggest going to other routes: Fix your CSS so that you are just adding HTML outside the Effects tag.
For example:
content = content.Replace ("the", "& lt; Span class = \ "Highlight \" & gt; & Lt; / Span & gt; ");
Then, in your CSS:
span.highlight {background color: yellow;} B span.highlight, I span.highlight, span.highlight, strong span.highlight, p span.highlight, blockquote span.highlight {background: none;}
Just add an exclusion for each HTML tag Whose content should not be highlighted.
Comments
Post a Comment