欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » ASP.Net » 正文

热门阅读

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

创建时间:2011年12月02日 17:32  阅读次数:(4785)
分享到:
如有一个字符串: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" onclick="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#加正则实现

高手们写出了这么多的实现方法,总有一个是适合我们的吧,我们要学习的,不仅仅是代码,还有解题思路。高手们的解题思路已经贴出来,供大家参考吧。錘蔛薔蛜孾購$N*NN槝T\:N'Y禰薔蛜俌UO
来源:CSDN
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 5+36=? 的结果(结果是:41)
结  果: