HTML转化为普通文本

HTML:

'<div id="upload-btn"><span class="upload-btn-title">上传文档</span><div class="upload-tip">支持pdf、word、ppt、excel、jpg等格式</div><div class="first-tip">支持中英日韩,不限字符翻译,免费导出</div></div>'

转为普通文本:

"上传文档支持pdf、word、ppt、excel、jpg等格式支持中英日韩,不限字符翻译,免费导出"

 

代码版本1:

function filterHTML(html){
    let s = html.replace(/<\/?.+?>/g, ""),
        obj = {
            "&nbsp;": " ", "&lt;": "<", "&gt;": ">", "&amp;": "&",
            "&quot;": '"', "&apos;": "'", "&cent;": "¢", "&pound;": "£",
            "&yen;": "¥", "&euro;": "€", "&sect;": "§", "&copy;": "©",
            "&reg;": "®", "&trade;": "™", "&times;": "×", "&divide;": "÷"
        },
        reg = /(&nbsp;|&lt;|&gt;|&amp;|&quot;|&apos;|&cent;|&pound;|&yen;|&euro;|&sect;|&copy;|&reg;|&trade;|&times;|&divide)/g;
    return s.replace(reg, function($){ return obj[$]; });
}

代码版本2:

function decoder(html){
    return ((e, html) => (e.innerHTML = html, e))(document.createElement('div'), html).textContent;
}

欢迎朋友们指出有所缺漏的地方

posted @ 2020-09-14 14:50  吕洋  阅读(421)  评论(0)    收藏  举报