请教html转txt的较好算法! 现在想做一个html转txt的工具,但是碰到一个比较大的问题就是对<script>脚本内容的处理,原来以为用正则表达式<script[^>]+>([^<]+)</script>这样就行了,但是后来才发现,如果碰到有些脚本里面还包含了<>标记的,就没办法了,不知道哪位大虾有好的办法吗?不用正则表达式也可以!谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最简单得方法是取网页body的innerText <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 <script[^>]*?>.*?</script> 谢谢以上的几位兄弟,但是鼠·神·泪兄的写法还是不行:(!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>></font> <a href=http://cul.sina.com.cn/ class=a02>文化</a> <font color=#3118ad>></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> <script.*?>[\n.]*?</script> <script.*?>.*?</script>打开单行模式,或者:(?s:script.*?>.*?</script>) Ivony兄,谢谢你,我上面的那个脚本是可以选择了,但是,如果是一个完整的页面,里面基本所有的内容都被选中了:( 类似于这个页面,把里面的脚本全部要去掉http://book.sina.com.cn/nzt/lit/zhengfashuji/3.shtml <script[^>]*?>.*?</script> 谢谢dlzhangln,终于解决问题! 这段代码是怎么的呢? dllimport中的各个属性什么意思? FORM.SHOW();和FORM_LOAD()问题 C#如何取消webBrowser浏览页面弹出的对话框? 关于在backgroundworker控件下创建子控件的问题 请问我一个线程可以导 x 条数据,总共有 M 条数据,怎么计算我需要开几个线程? 执行存储过程问题 菜鸟求助:水晶报表分页的问题 一个奇怪的C#OO问题 誰來告訴我C#中的消息泵機制? 50分求答,用HttpWebRequest的时候被closed. 求算法
http://blog.csdn.net/www_123du_com/archive/2006/09/06/1184569.aspx
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>></font> <a href=http://cul.sina.com.cn/ class=a02>文化</a> <font color=#3118ad>></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>
打开单行模式,或者:
(?s:script.*?>.*?</script>)
http://book.sina.com.cn/nzt/lit/zhengfashuji/3.shtml