Javascript:无法从字符串中去除<script>标签

时间:2019-11-06 12:04:34

标签: javascript regex

我正在尝试从HTML字符串中剥离标签

除了字符串中的<script>标记会破坏功能之外,一切都很好。

在这里:

function stripHTML(value) {
    // remove script tags
    value = value.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');

    // Convert `&amp;` to `&`.
    value = value.replace(/&amp;/gi, '&');

    // Replace spaces.
    value = value.replace(/&nbsp;/gi, ' ');
    value = value.replace(/\s+/g, ' ');

    // Remove "<b>".
    value = value.replace(/<b[^r>]*>/gi, ''); // do not mess with <br>!
    value = value.replace(/<\/b>/gi, '');

    // Remove "<strong>".
    value = value.replace(/<strong[^>]*>/gi, '');
    value = value.replace(/<\/strong>/gi, '');

    // Remove "<i>".
    value = value.replace(/<i[^>]*>/gi, '');
    value = value.replace(/<\/i>/gi, '');

    // Remove "<em>".
    value = value.replace(/<em[^>]*>/gi, '');
    value = value.replace(/<\/em>/gi, '');

    // Remove "<u>".
    value = value.replace(/<u[^>]*>/gi, '');
    value = value.replace(/<\/u>/gi, '');

    // Tighten up "<" and ">".
    value = value.replace(/>\s+/g, '>');
    value = value.replace(/\s+</g, '<');

    // Replace "<br>".
    //value = value.replace(/<br>/gi, '\n');

    // Replace "<div>" (from Chrome).
    value = value.replace(/<div[^>]*>/gi, '');
    value = value.replace(/<\/div>/gi, '<br><br>');

    // Replace "<p>" (from IE).
    value = value.replace(/<p[^>]*>/gi, '');
    value = value.replace(/<\/p>/gi, '<br><br>');

    // Replace "<h*>" (from IE).
    value = value.replace(/<h[^>]*>/gi, '');
    value = value.replace(/<\/h[^>]>/gi, '<br><br>');

    // No more than 2x newline, per "paragraph".
    //value = value.replace(/\n\n+/g, '\n\n');
    value = value.replace(/<br><br>+/g, '<br><br>');

    // remove trailing <br>s
    value = value.replace(/(<br\s*\/?\s*>\s*)+$/g, '');

    // Whitespace before/after.
    value = $.trim(value);

    return value;
};

然后here is a fiddle显示正在发生的事情(如果从字符串中删除<script>alert("gianni")</script>部分,一切正常)

请帮忙吗?

0 个答案:

没有答案