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

由网友(劣徒     ゅ)分享简介:我在输出搜索结果列表的关键字给定的字符串,我想在我的搜索结果中任何匹配的关键字高亮显示。每个字都应该被包在一个范围或相似。我在寻找一个有效的功能来做到这一点。I am outputting a list of search results for a given string of keywords, and I w...

我在输出搜索结果列表的关键字给定的字符串,我想在我的搜索结果中任何匹配的关键字高亮显示。每个字都应该被包在一个范围或相似。我在寻找一个有效的功能来做到这一点。

I am outputting a list of search results for a given string of keywords, and I want any matching keywords in my search results to be highlighted. Each word should be wrapped in a span or similar. I am looking for an efficient function to do this.

例如。

关键词:Lorem存有

Keywords: "lorem ipsum"

结果:含有LOREM和存有一些文本

Result: "Some text containing lorem and ipsum"

所需的HTML输出:包含一些文字<跨度类=打> LOREM< / SPAN>和<跨度类=打>存有< / SPAN>

我的结果是不区分大小写。

My results are case insensitive.

推荐答案

下面是我决定的。一个扩展功能,我可以在我的网页我的网页/节相关的字符串拨打:

Here's what I've decided on. An extension function that I can call on the relevant strings within my page / section of my page:

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;
}

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

Any further suggestions or comments?

阅读全文

相关推荐

最新文章