javascript去除HTML标签
//str 传入的html源文件 noEnter 表示是否去除换行// 不全的话 可依据格式 自己添加.
function html2txt(str,noEnter){
var html = str;
html = html.replace(/<!--[\s\S]*?-->/img, "");//注释
html = html.replace(/<[\/]*table[^>]*>/img, "\n");//table
html = html.replace(/<[\/]*tbody[^>]*>/img, "");//tbody
html = html.replace(/<[\/]*tr[^>]*>/img, "\n");//tr
html = html.replace(/<[\/]*td[^>]*>/img, "\n");//td
html = html.replace(/<[\/]*p[^>]*>/img, "\n");//p
html = html.replace(/<[\/]*a[^>]*>/img, "\n");//a
html = html.replace(/<[\/]*col[^>]*>/img, "\n");//col
html = html.replace(/<[\/]*br[^>]*>/img, "\n");//br
html = html.replace(/<[\/]*[^>]*>/img, "\n");//
html = html.replace(/<[\/]*span[^>]*>/img, "");//span
html = html.replace(/<[\/]*center[^>]*>/img, "");//center
html = html.replace(/<[\/]*ul[^>]*>/img, "");//ul
html = html.replace(/<[\/]*i[^>]*>/img, "");//i
html = html.replace(/<[\/]*li[^>]*>/img, "");//li
html = html.replace(/<[\/]*b[^>]*>/img, "");//b
html = html.replace(/<[\/]*hr[^>]*>/img, "");//hr
html = html.replace(/<[\/]*h\d+[^>]*>/img, "");//h1,2,3,4,5,6
html = html.replace(/<STYLE[\s\S]*?<\/STYLE>/img, "");//样式
html = html.replace(/<script[\s\S]*?<\/script>/img, "");//引用的脚本
//html = html.replace(/<[\?!A-Za-z\][^><]*>/img, "");alert("str:"+html)
html = html.replace(/\r/img, "");//换行
html = html.replace(/\n/img, "\r\n");//回车
html = html.replace(/[ |\s]*\r\n[ |\s]*\r\n/img, "\r\n");
//html = reg.replace(html,@"(\r\n)[^ ]/img,"$1");
html = formatHtml(html);
if(noEnter){
html = html.replace(/\r\n/img, "");
html = html.replace(/\n/img, "");
html = html.replace(/\r/img, "");
}
return (html);
}