0

Hi,

I am using following code to Highlighting keywords in a text or resume.

My code:

// al is an Arraylist which have keywrods as .net, java, xml...

al = (ArrayList)Session["keyWords"];

for (int i = 0; i < al.Count; i++)
                    {
                        if (s.Contains(al[i].ToString().ToLower()) || s.Contains(al[i].ToString().ToUpper()) || s.Contains(al[i].ToString()))
                        {
                            //s = s.Replace(al[i].ToString(), "<b><font color='red'>" + al[i] + "</font></b>");
                            s = HighlightKeywords(s, al[i].ToString());
                        }
                    }


private static string HighlightKeywords(string text,string keywords)
    {
        // Swap out the ,<space> for pipes and add the braces
        Regex r = new Regex(@", ?");
        keywords = "(" + r.Replace(keywords, @"|") + ")";

        // Get ready to replace the keywords
        r = new Regex(keywords, RegexOptions.Singleline | RegexOptions.IgnoreCase);

        // Do the replace
        return r.Replace(text, new MatchEvaluator(MatchEval));
    }

private static string MatchEval(Match match)
    {
        if (match.Groups[1].Success)
        {
            return "<b><font color='red'>" + match.ToString() + "</font></b>";

        }

        return ""; //no match
    }

If i search .net,xml,java then it does not show java in JavaScript or Java.

please help me

Thanks in advance

Pankaj

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Pankaj18
0

Hi,

I am using following code to Highlighting keywords in a text or resume.

My code:

string keywords = ".net";
        string text = "These are technologies: .net, intranet, NET, net,ASP.net.";

        Response.Write(HighlightKeywords(keywords, text));



private static string HighlightKeywords(string text,string keywords)
    {
        // Swap out the ,<space> for pipes and add the braces
        Regex r = new Regex(@", ?");
        keywords = "(" + r.Replace(keywords, @"|") + ")";

        // Get ready to replace the keywords
        r = new Regex(keywords, RegexOptions.Singleline | RegexOptions.IgnoreCase);

        // Do the replace
        return r.Replace(text, new MatchEvaluator(MatchEval));
    }

private static string MatchEval(Match match)
    {
        if (match.Groups[1].Success)
        {
            return "<b><font color='red'>" + match.ToString() + "</font></b>";

        }

        return ""; //no match
    }

The output should be shown as

These are technologies: .net, intranet, NET, net,ASP.net.

but it showing as:

These are technologies: .net, intranet, NET, net,ASP.net.

please help me

Thanks in advance

Pankaj

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.