美文网首页
剥去字符串中的 HTML 标签

剥去字符串中的 HTML 标签

作者: 青城墨阕 | 来源:发表于2020-05-12 19:38 被阅读0次
stripTags(val) {
    return val.replace(/<\/?[^>]+>/gi, '');
}
let htmlStr = '<font style="font-family: 微软雅黑; text-align: left; color: rgb(0, 0, 0);"><font style="font-family: 微软雅黑; text-align: left; color: rgb(0, 0, 0);">hi <a style="display: inline;" href="http://www.baidu.com" id="curr-1589196169191" class="link">hi</a>那你不<img src="http://s.goutong.baidu.com/static/ueditor/images/371335731f814e778a6f47ea4cff900e.jpg" style="font-family: 微软雅黑; text-align: left; color: rgb(0, 0, 0);">不不<a style="display: inline;" href="http://www.baidu.a.com" id="curr-1589196203777" class="link">不</a><font style="font-family: 微软雅黑; text-align: left; color: rgb(0, 0, 0);"></font></font></font>'

let tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlStr;

let a;
while (a = tempDiv.getElementsByTagName('a'), a.length > 0) {
    a[0].outerHTML = '[链接]';
}

while (a = tempDiv.getElementsByTagName('img'), a.length > 0) {
    a[0].outerHTML = '[图片]';
}

while (a = tempDiv.getElementsByTagName('table'), a.length > 0) {
    a[0].outerHTML = '[表格]';
}

let poorContent = this.stripTags(tempDiv.innerHTML);
// hi [链接]那你不[图片]不不[链接]

相关文章

网友评论

      本文标题:剥去字符串中的 HTML 标签

      本文链接:https://www.haomeiwen.com/subject/dnyinhtx.html