美文网首页
JS去除HTML标签

JS去除HTML标签

作者: 时光已翩然轻擦 | 来源:发表于2021-10-27 14:42 被阅读0次

方法

 function removeHTMLTag(str) {
    str = str.replace(/<\/?[^>]*>/g, '') // 去除HTML tag
    str = str.replace(/[ | ]*\n/g, '\n') // 去除行尾空白
    str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
    str = str.replace(/ /ig, '') // 去掉
    const arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' } // 转义符换成普通字符
    str = str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) { return arrEntities[t] })
    return str
  }

参考资料

相关文章

网友评论

      本文标题:JS去除HTML标签

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