日期:2014-05-17  浏览次数:20799 次

javascript去除HTML标签

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, "")