现在想做一个html转txt的工具,但是碰到一个比较大的问题就是对<script>脚本内容的处理,原来以为用正则表达式<script[^>]+>([^<]+)</script>这样就行了,但是后来才发现,如果碰到有些脚本里面还包含了<>标记的,就没办法了,不知道哪位大虾有好的办法吗?不用正则表达式也可以!谢谢!

解决方案 »

  1.   

    最简单得方法是取网页body的innerText
      

  2.   

    <script[^>]*>((?!\</script\>).(?<!\</script\>))*</script>1.不要随便加括号,对结果不会有什么影响,但会影响性能和可读性。2.HTML解析也是以</script>作为其结尾标志,所以,见到</script>就表示一定结束了。也由于这个原因,写JS时如果字符串中要包含</script>,必须拆开,比如写成:"</"+"script>"3.((?!\</script\>).(?<!\</script\>))*这种写法参见我的blog:
    http://blog.csdn.net/www_123du_com/archive/2006/09/06/1184569.aspx
      

  3.   

    <script[^>]*?>.*?</script>
      

  4.   

    谢谢以上的几位兄弟,但是鼠·神·泪兄的写法还是不行:(!winner兄的写法也没办法匹配,真是晕阿!我把那些脚本贴出来,大家看看是不是还有什么别的写法能匹配的!<script language="JavaScript">
    function doZoom(size){
    document.getElementById('zoom').style.fontSize=size+'px'
             setTailPosition()
    }function doPrint(){
    var str="<html>\n<meta http-equiv='content-type' content='text/html; charset=gb2312'>";
    var article;
    var css;
    var strAdBegin="<!--NEWSZW_HZH_BEGIN-->";
    var strAdEnd="<!--NEWSZW_HZH_END-->";
    var strFontSize='【<a href="javascript:doZoom(16)">大</a> <a href="javascript:doZoom(14)">中</a> <a href="javascript:doZoom(12)">小</a>】'.toLowerCase()
    var strdoPrint="doPrint()";
    var strTmp; css="<style>"
    +"td,.f12{font-size:12px}"
    +"body{font-family:宋体}"
    +".f24 {font-size:24px;}"
    +".f14 {font-size:14px;}"
    +".a {font-size:14.9px;line-height:130%}"
    +".title14 {font-size:14px;line-height:130%}"
    +".l17 {line-height:170%;}"
    +".x14 {font-size:14px;line-height:130%}"
             +"a.a02:link,a.a02:active,a.a02:visited,a.a02:hover{text-decoration:none;color: #311bad;}"
    +"a.htt:link,a.htt:active,a.htt:visited{text-decoration:none;color: #7b4109;}"
    +"</style>"; str += css;
    str += '<meta http-equiv="content-type" content="text/html; charset=gb2312">';
    str += '<title>'+document.title+'</title>';
    str += "<body bgcolor=#ffffff topmargin=5 leftmargin=5 marginheight=5 marginwidth=5 onLoad='window.print()'>";
    str += "<center><table width=600 border=0 cellspacing=0 cellpadding=0><tr><td height=34 width=150><a href=http://cul.sina.com.cn><img src=http://image2.sina.com.cn/lit/images/cul_logo.gif width=118 height=31 border=0></a></td><td valign=bottom><a href=http://www.sina.com.cn class=a02 target=_blank>新浪首页</a> <font color=#3118ad>&gt;</font> <a href=http://cul.sina.com.cn/ class=a02>文化</a> <font color=#3118ad>&gt;</font><font color=#898a8e>正文</font>"
    str += "</td><td align=right valign=bottom><a href='javascript:history.back()'>返回</a> <a href='javascript:window.print()'>打印</a></td></tr></table>";
    str += "<table width=600 border=0 cellpadding=0 cellspacing=20 bgcolor=#F2F4F2><tr><td>";

    article=document.getElementById('article').innerHTML;
    if(article.indexOf(strAdBegin)!=-1){
    str +=article.substr(0,article.indexOf(strAdBegin));
    strTmp=article.substr(article.indexOf(strAdEnd)+strAdEnd.length, article.length);
    }else{
    strTmp=article
    }
    str +=strTmp
    str += window.location.href
    str += "</td></tr></table></center>";
    str += "</body></html>";
    document.write(str);
    document.close();
    }
    </script>
      

  5.   

    <script.*?>[\n.]*?</script>
      

  6.   

    <script.*?>.*?</script>
    打开单行模式,或者:
    (?s:script.*?>.*?</script>)
      

  7.   

    Ivony兄,谢谢你,我上面的那个脚本是可以选择了,但是,如果是一个完整的页面,里面基本所有的内容都被选中了:(
      

  8.   

    类似于这个页面,把里面的脚本全部要去掉
    http://book.sina.com.cn/nzt/lit/zhengfashuji/3.shtml
      

  9.   

    <script[^>]*?>.*?</script>
      

  10.   

    谢谢dlzhangln,终于解决问题!