【js去除html标签】兼容浏览器只提取文字

function getPlainTxt(html) {
        var DEFAULT_VERSION = 8.0;
        var ua = navigator.userAgent.toLowerCase();
        var isIE = ua.indexOf("msie") > -1;
        var safariVersion;
        if (isIE) {
            safariVersion = ua.match(/msie ([\d.]+)/)[1];
        }
        if (safariVersion <= DEFAULT_VERSION) {
            html = html.replace(/[\n\r]/g, ''); //ie要先去了\n在处理
            html = html.replace(/<([a-zA-Z1-6]+)(\s*[^>]*)?>/g, "<$1>");
            html = html.replace(/<(?!\/?P|\/?BR)[^<>]*>/ig, '');
            html = $.trim(html, "g");
            html = html.replace(/(

| |

|<\/pre>)/g, "");
            html = html.replace(/(<\/P>|
)/g, "\n"); html = html.replace(/\//g,""); html = html.replace(/\s/g,""); } else { html = html.replace(/<([a-zA-Z1-6]+)(\s*[^>]*)?>/g, "<$1>"); html = html.replace(/<(?!\/?p|\/?br)[^<>]*>/ig, ''); html = $.trim(html, "g"); html = html.replace(/(

| |

|<\/pre>)/g, "");
            html = html.replace(/(<\/p>|
)/g, "\n"); html = html.replace(/\//g,""); html = html.replace(/\s/g,""); } return html; }

 

你可能感兴趣的:(自用js)