I have a simple search & replace function which find & replaces the search keyword with keyword wrapped around <span></span>
tag so that match is highlighted in different color
sample code
private string GetHighlightedText(string text, string keyword)
{
if (keyword != null)
{
Regex regex = null;
string pattern = @"(\b(?:" + keyword + @")\b)(?![^<]*?>)";
regex = new Regex(pattern, RegexOptions.IgnoreCase);
text = regex.Replace(text, "<span class='keyword-highlight'>$1</span>");
}
return text;
}
This function at moment matches the whole world & replaces the match with the <span class='keyword-highlight'>$1</span>
let us say i am looking for for Global Villa
then it should also highlight part of text Global Village
as <span class='keyword-highlight'>Global Villa</span>ge