I have written a function which works with my sites search functionality. When the user searches a word, I perform a replace on the returned search content to take any word that the user entered into the search, and wrap it in span tags with a custom class which will basically bold the word on the page. After overcoming my first road block of having to incorporate case-insensitive replacements, I'm now stuck in another predicament. The word that is replaced on the page is being replaced with the users provided case on the page which looks funny because the content returned is a lot of legal text and acronyms. If a user were to search "rpC 178", the "RPC 178" in the content is displayed as bold and the same case "rpC 178". My first thought was to split the content by "space" and keep a temporary copy of the replaced word before it's replaced in order to preserve it's current case but some of these content blocks can be upwards of 4000 words so that seems inefficient. Am I going about this the wrong way?
Here is my current code:
public static String HighlightWords(String content, String className, String searchTerms)
{
string[] terms = new string[] { };
if (!string.IsNullOrWhiteSpace(searchTerms))
{
terms = searchTerms.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
if (terms == null || terms.Length == 0)
{
return content;
}
var optimizedContent = new StringBuilder(content);
var startHtml = string.Format("<span class=\"{0}\">", className);
var endHtml = "</span>";
string result = string.Empty;
foreach (var term in terms)
{
result = Regex.Replace(optimizedContent.ToString(), term, string.Format("{0}" + term + "{1}", startHtml, endHtml), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
return result;
}