js将html转换为纯文本

document.body.textContent//firefox浏览器

document.body.innerText//适用ie webkit浏览器

document.body.innerHTML.replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi,'').replace(/<[^>]+?>/g,'').replace(/\s+/g,' ').replace(/ /g,' ').replace(/>/g,' ');


repalceHtmlToText = (str) => {

    str = str.replace(/<\/?.+?>/g, "");

    str = str.replace(/ /g, "");

    return str;

}

你可能感兴趣的:(js将html转换为纯文本)