Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Friday, September 14, 2007

Highlight search text functions using css tags and regex



protected string HighlightText(string searchWord, string inputText)
{

Regex expression = new Regex(searchWord.Replace(" ", "|"));
// Regex.ignorecase

return expression.Replace(inputText, new MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m)
{
return "<span class='highlight'>" + m.Value + "</span>";
}

No comments: