记录使用正则去掉HTML标签和HTML转义
// 去除标签
setText:function(html) {
return html.replace(/<(p|div)[^>]*>(<br\/?>| )<\/\1>/gi, '\n')
.replace(/<br\/?>/gi, '\n')
.replace(/<[^>/]+>/g, '')
.replace(/(\n)?<\/([^>]+)>/g, '')
.replace(/\u00a0/g, ' ')
.replace(/ /g, ' ')
.replace(/<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>/g, '')
.replace(/<\/?(img|table)[^>]*>/g, "") // 去除图片和表格
.replace(/<\/?(a)[^>]*>/g, "") // 去除a标签
};
// 处理转义的HTML
transferHtml:function(html) {
let reg = /(<)|(>)|(&)|(")/g;
return html ? html.replace(reg,function(match){
switch(match){
case "<":
return "<";
case ">":
return ">"
case "&":
return "&";
case """:
return "\"";
}
}) : '';
};