在ASP.Net中搜索关键字高亮显示

Tim*_*imS 7 .net c# asp.net search highlighting

我正在输出给定字符串关键字的搜索结果列表,我希望我的搜索结果中的任何匹配关键字都会突出显示.每个单词应包含在跨度或类似内容中.我正在寻找一个有效的功能来做到这一点.

例如

关键词:"lorem ipsum"

结果:"一些包含lorem和ipsum的文本"

期望的HTML输出:" Some text containing <span class="hit">lorem</span> and <span class="hit">ipsum</span>"

我的结果不区分大小写.

Tim*_*imS 13

这是我决定的.我可以在页面/页面部分中的相关字符串上调用的扩展函数:

public static string HighlightKeywords(this string input, string keywords)
{
    if (input == string.Empty || keywords == string.Empty)
    {
        return input;
    }

    string[] sKeywords = keywords.Split(' ');
    foreach (string sKeyword in sKeywords)
    {
        try
        {
            input = Regex.Replace(input, sKeyword, string.Format("<span class=\"hit\">{0}</span>", "$0"), RegexOptions.IgnoreCase);
        }
        catch
        {
            //
        }
    }
    return input;
}
Run Code Online (Sandbox Code Playgroud)

任何进一步的建议或意见?