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 = { " ": " ", "<": "<", ">": ">", "&": "&", """: '"', "'": "'", "¢": "¢", "£": "£", "¥": "¥", "€": "€", "§": "§", "©": "©", "®": "®", "™": "™", "×": "×", "÷": "÷" }, reg = /( |<|>|&|"|'|¢|£|¥|€|§|©|®|™|×|÷)/g; return s.replace(reg, function($){ return obj[$]; }); }
代码版本2:
function decoder(html){ return ((e, html) => (e.innerHTML = html, e))(document.createElement('div'), html).textContent;
}
欢迎朋友们指出有所缺漏的地方

浙公网安备 33010602011771号