实现在搜索后将结果中的关键字高亮显示

如有一个字符串:str="<span >这个是测试<a href='http://www.lmwlove.com/ac/ID280' >280</a >这里是其它测试</span >"

现在用户搜索的是280,这时候就要把上面这个字符串变为:
str="<span >这个是测试<a href='http://www.lmwlove.com/ac/ID280' ><label style='color:red' >280</label ></a >这里是其它测试</span >",需要注意的是,我的href中也有280这个关键字,但这个280是不能够被替换的。

csdn上高人的解决方法,都复制如下,有些并不能完全解决这个问题,但我觉的还是有学习的价值,也粘贴如下,各回复用--隔开

-------------------------------------------------------------
public static string replacered(string title, string redkey)
    {
        title = title.Replace(redkey, "<font color='#ff0000' >" + redkey + "</font >");
        return title;
    }

这个是用C#实现的

-------------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml"  >
<head runat="server" >
</head >
<body >
    <form id="form1" runat="server" >
    <div >
    <a href='#?id=280' id="a1" >280</a >
    <script >    document.getElementById("a1").innerHTML=document.getElementById("a1").innerHTML.replace("280","<font color='red' >280</font >");
    </script >
    </div >
    </form >
</body >
</html >

这个是用js实现的

-------------------------------------------------------------
string AV2Color(string tmpStr)
  {
  string reValue = tmpStr;
  string xingcheng = @"([a-zA-Z]{6})";
  reValue = Regex.Replace(reValue, xingcheng, "<font color=yellow >$1</font >");
  }

这个是在C#中调用js实现

-------------------------------------------------------------
string s = "<a href='#?id=280' ><label class='sr_word01' >280 </label > </a >";  
string r = @"( >[ \s\S]*)(280)([ \s\S]*<)";  
string v = Regex.Replace(s, r, "$1<font color=red >$2</font >$3");  
Response.Write(v); 

这个是使用正则实现

-------------------------------------------------------------
<html >
    <head >
        <title >IE Range Example</title >
        <script type="text/javascript" >
            function useRanges() {
                var oRange = document.body.createTextRange();
                var oP1 = document.getElementById("p1");
                oRange.findText("Hello");
                oRange.pasteHTML("<em >Howdy</em >");
            }
        </script >
    </head >
    <body ><p id="p1" ><b >Hello</b > World</p >
        <input type="button" value="Use Ranges" οnclick="useRanges()" / >  
        <p ><strong >Note:</strong > This example uses Internet Explorer ranges and will only work on Internet Explorer.</p >      
    </body >
</html >

这个是使用jquery实现

-------------------------------------------------------------
private static string HLSearchWords(string word)
            /// ----分析keyword,空格分离
        {
            word = Regex.Replace(word, @"(\"")|(\()|(\))|(\+)|(\|)|(-)|(“)|(”)|(()|())|(+)|(-)|(\\)|(\*)|(\<)|(\ >)|( )", " ");
            /*word = word.Replace("(", " ");
            word = word.Replace(")", " ");
            word = word.Replace("-", " ");
            word = word.Replace("|", " ");*/
            word = Regex.Replace(word, @"\s+", " ");
            return word.Trim();
        }
        public static string HLTextDisplay(string strWord, string strSource)
        {
                strSource = System.Text.RegularExpressions.Regex.Replace(strSource, @"<\w+[^< >]* >|<\/\w+[^< >]* >", "");
                strSource = System.Text.RegularExpressions.Regex.Replace(strSource, "\r\n", @"<br >");
                if (strWord==null || strWord.Length==0) return strSource;
                strWord = HLSearchWords(strWord);
                string strTemp=strSource;
                strWord = "(" + Regex.Replace(strWord, @"\s+", ")|(") + ")";
                strTemp = Regex.Replace(strTemp, strWord, "<font color=color:#00c >$&</font >", RegexOptions.IgnoreCase);
                return strTemp;
        }

这个是使用C#加正则实现

高手们写出了这么多的实现方法,总有一个是适合我们的吧,我们要学习的,不仅仅是代码,还有解题思路。高手们的解题思路已经贴出来,供大家参考吧。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值