What I want to do is some "forbidden words" highlighting.
Here are the values ββthat I have:
I have a list of forbidden words in an array
{ "word1", "word2", "word3", "word4" }
I have a line representing a comment
"i want to word1ban this word3 stupidword4 comment"
I want to highlight them in html bold tags ( <b> </b> ). So, for example, this comment line will look like this:
"i want to <b>word1</b>ban this <b>word3</b> stupid<b>word4</b> comment"
As I actually do this, a regular expression replacement is used, and it works very well, except for one thing that annoys me.
foreach (var word in words) { value = Regex.Replace(value, string.Format(@"{0}", Regex.Escape(HttpUtility.HtmlEncode(word))), "<b>" + word + "</b>", RegexOptions.IgnoreCase); }
The problem with this, and also depends on the word order in the array, is that one of the forbidden words will affect your replacement ( <b> or </b> )
For example, if you add this to forbidden words: <b
Following the code, the first result of the iteration will be:
"i want to <b>word1</b>ban this <b>word3</b> stupid<b>word4</b> comment"
Then replace <b following:
"i want to <b><b</b>>word1</b>ban this <b><b</b>>word3</b> stupid<b><b</b>>word4</b> comment"
I do not want to influence my replacement. I am wondering how we can do this. I tried adding exceptions to my regex to exclude <b> and </b> in replacement without success.