从Text中删除HTML

时间:2014-03-28 10:41:25

标签: javascript jquery html

我正在从我需要的文本周围的HTML服务器返回一些文本。使用javascript / jquery,删除所有html标记的最佳方法是,所有我记录或保存的只是文本:"这是会议的正文和文本。"

谢谢 - 下面是html的样子。我一直在坚持回归

> <html><head><meta http-equiv="Content-Type" content="text/html;
> charset=utf-8"><meta name="Generator" content="Microsoft Exchange
> Server"><!-- converted from rtf --><style><!-- .EmailQuote {
> margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; }
> --></style></head><body><font face="Calibri" size="2"><span style="font-size:11pt;">             <div>This is the body and text of the
> meeting.</div><div>&nbsp;</div></span></font></body></html>

5 个答案:

答案 0 :(得分:4)

使用正则表达式,您可以从yourtext中删除html标记。

的javascript:

    $('textarea').on('input', function (){
    $('div').text(sanitize(this.value));
});

var protos = document.body.constructor === window.HTMLBodyElement;
validHTMLTags  =/^(?:a|abbr|acronym|address|applet|area|article|aside|audio|b|base|basefont|bdi|bdo|bgsound|big|blink|blockquote|body|br|button|canvas|caption|center|cite|code|col|colgroup|data|datalist|dd|del|details|dfn|dir|div|dl|dt|em|embed|fieldset|figcaption|figure|font|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hgroup|hr|html|i|iframe|img|input|ins|isindex|kbd|keygen|label|legend|li|link|listing|main|map|mark|marquee|menu|menuitem|meta|meter|nav|nobr|noframes|noscript|object|ol|optgroup|option|output|p|param|plaintext|pre|progress|q|rp|rt|ruby|s|samp|script|section|select|small|source|spacer|span|strike|strong|style|sub|summary|sup|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video|wbr|xmp)$/i;

function sanitize(txt) {
var // This regex normalises anything between quotes
    normaliseQuotes = /=(["'])(?=[^\1]*[<>])[^\1]*\1/g,
    normaliseFn = function ($0, q, sym) { 
        return $0.replace(/</g, '&lt;').replace(/>/g, '&gt;'); 
    },
    replaceInvalid = function ($0, tag, off, txt) {
        var 
            // Is it a valid tag?
            invalidTag = protos && 
                document.createElement(tag) instanceof HTMLUnknownElement
             || !validHTMLTags.test(tag),
            isComplete = txt.slice(off+1).search(/^[^<]+>/) > -1;

        return invalidTag || !isComplete ? '&lt;' + tag : $0;
    };

txt = txt.replace(normaliseQuotes, normaliseFn)
         .replace(/<(\w+)/g, replaceInvalid);

var tmp = document.createElement("DIV");
tmp.innerHTML = txt;

return "textContent" in tmp ? tmp.textContent : tmp.innerHTML;
}

HTML:

Enter some HTML here, invalid tags aren't removed:<br>


输出:

答案 1 :(得分:2)

JS:

string.replace(/(<([^>]+)>)/ig,"");

Jquery(可能更慢):

string.text();

答案 2 :(得分:1)

你是说这个?

var yourtext = '<html><head><meta http-equiv="Content-Type" content="text/html;'+
'> charset=utf-8"><meta name="Generator" content="Microsoft Exchange'+
'> Server"><!-- converted from rtf --><style><!-- .EmailQuote {'+
'> margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; }'+
'> --></style></head><body><font face="Calibri" size="2"><span style="font-size:11pt;">      '+                   '<div>This is the body and text of the'+
'> meeting.</div><div>&nbsp;</div></span></font></body></html>';

var textWithoutHtml = $(yourtext).children().text();

答案 3 :(得分:0)

使用jquery:

$(yourtext).text();

答案 4 :(得分:0)

  1. 使用AJAX或您正在使用的任何内容获取服务器响应。
  2. 在标记内存储服务器响应。
  3. 使用jQuery或.text()使用jQuery获取该div的外部文本。
  4. 请参阅工作示例http // jsfiddle.net / imdadhusen / Hs4Q6 / 7 /