<html>
<body>
<textarea id="textarea1" style="height:240;width:960">
前部分关键词后部分                                            //替换
<a href="#">前部分关键词后部分</a>                                      //不替换,包含在a中
<p><a href="#">前部分关键词后部分</a></p>                               //不替换,包含在a中
<div><p><a href="#">前部分关键词后部分</a></p></div>                    //不替换,包含在a中
<span><div><p><a href="#">前部分关键词后部分</a></p></div></span>       //不替换,包含在a中
<a><span><div>前部分关键词后部分</div></span></a>                       //不替换,包含在a中
<font><a href="#">前部分关键词后部分</a>前部分关键词后部分</font>       //只替换第二部分的关键词
<span>前部分关键词后部分<div>前部分关键词后部分</div></span>         //两部分都要替换<img alt="前部分关键词后部分" />                                        //不替换,包含在img中
<span><img alt="前部分关键词后部分" /></span>                           //不替换,包含在img中
<div><span><img alt="前部分关键词后部分" /></span></div>                //不替换,包含在img中<script>alert("前部分关键词后部分");</script>                           //不替换,包含在script中
<span><script>alert("前部分关键词后部分");</script></span>              //不替换,包含在script中
<div><span><script>alert("前部分关键词后部分");</script></span></div>   //不替换,包含在script中
<span><div><script>alert("前部分关键词后部分");</script></div></span>   //不替换,包含在script中<div>前部分关键词后部分</div>                                           //替换
<span><div>前部分关键词后部分</div></span>                              //替换
</textarea>
<textarea id="textarea2" style="height:240;width:960"></textarea>
<script>
var str=document.getElementById("textarea1").value
var keyword = "关键词";
var myreplace="@#@@"
var re = new RegExp("(.*)?(<a.*>)(.*)"+keyword+"(.*)(<\/a>.*)","ig");
str = str.replace(re,"$1$2$3"+myreplace+"$4$5");
var re = new RegExp("(.*)?(<script.*>)(.*)"+keyword+"(.*)(<\/script>.*)","ig");
str = str.replace(re,"$1$2$3"+myreplace+"$4$5");
var re = new RegExp("(.*)?(<img.*)(.*)"+keyword+"(.*)(\/>.*)","ig");
str = str.replace(re,"$1$2$3"+myreplace+"$4$5");
var re= new RegExp("(.*?)"+keyword+"(.*?)","ig");
str = str.replace(re,"$1<b>"+keyword+"</b>$2");
var re= new RegExp("(.*?)"+myreplace+"(.*?)","ig");
str = str.replace(re,"$1"+keyword+"$2");document.getElementById("textarea2").value =str</script>
</body>
</html>
我测试通过了,你看看还有没有其他的特例。

解决方案 »

  1.   

    完整的代码是这样的:
    function test(htmstr,key) {re = new RegExp("("+key+")","ig");
    restr = "<span style=\"color:red\">$1<\/span>";htmstr = htmstr.replace(re,restr);
    return htmstr;
    }htmstr是要innerHTML到WEB页面上的,只是要避免htmstr中的HTML标记中的任意字符不被替换。
      

  2.   

    参考http://community.csdn.net/Expert/topic/5141/5141765.xml?temp=.3382685
      

  3.   

    关键字为:http
    <a href=http://xxx.xxx.xxx>http://xxx.xxx.xxx</a>
    <a href=http://xxx.xxx.xxx><span style="color:red">http</span>://xxx.xxx.xxx</a>//“<>”中的http没有被替换,“<a></a>”之间的http被替换。关键字为:Qiaorui
    <img src=ftp.gif href=ftp://ftp.Qiaorui.net>ftp://ftp.Qiaorui.net</img>
    <img src=ftp.gif href=ftp://ftp.Qiaorui.net>ftp://ftp.<span style="color:red">Qiaorui</span>.net</img>//“<>”中的Qiaorui没有被替换,“<img></img>”之间的Qiaorui被替换。关键字为:div
    <div>DIV是HTML的标记</div>
    <div><span style="color:red">DIV</span>是HTML的标记</div>//“<>”之间的div没有被替换,“<div></div>”中的DIV被替换。偶要的就是这种效果。
      

  4.   

    <script type="text/javascript">
    function re(a, b, c) {
    if (b) return b
    else return nkey;
    }
    function repl(k, s) {
    var p = new RegExp("(<[^>]*?" + k + "[^>]*>)|(" + k + ")", "gi");
    var j = s.replace(p, re);
    alert(j);
    }
    var key = ["http", "Qiaorui", "DIV"];
    var nkey = "wc";
    repl(key[0], '<a href=http://xxx.xxx.xxx>http://xxx.xxx.xxx</a><a href=http://xxx.xxx.xxx><span style="color:red">http</span>://xxx.xxx.xxx</a>//“<>”中的http没有被替换,“<a></a>”之间的http被替换。');
    repl(key[1], "<img src=ftp.gif href=ftp://ftp.Qiaorui.net>ftp://ftp.Qiaorui.net</img>");
    repl(key[2], '<div>DIV是HTML的标记</div><div><span style="color:red">DIV</span>');
    </script>
      

  5.   

    <a href=http://xxx.xxx.xxx>http://xxx.xxx.xxx</a>
    <a>中的http正确,但<a>之间的http没了:)
      

  6.   

    第一个形式参数a是找到的所有内容。。
    第二个形式参数b是匹配这个(<[^>]*?" + k + "[^>]*>)的内容。。
    第三个形式参数c是匹配这个(" + k + ")的内容。。
    因为b和c不可能同时出现所以就分开判断。。如果匹配b那么就返回所有,其实应该返回a忽忽。。不过那样写也正确。。
    如果匹配c就说明它不在标记中。所以就返回关键字