使用正则表达式删除所有 html 属性(替换)

Remove all html attributes with regex (replace)

本文关键字:属性 替换 html 正则表达式 删除      更新时间:2023-09-26

例如,我有这样的html:

<title>Ololo - text’s life</title><div class="page-wrap"><div class="ng-scope"><div class="modal custom article ng-scope in" id="new-article" aria-hidden="false" style="display: block;"><div class="modal-dialog first-modal-wrapper">< div class="modal-content"><div class="modal-body full long"><div class="form-group">olololo<ul style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);"><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li></ul><p style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);">bbcvbcvbcvbcvbcvbcvbcvb</p></div></div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div class="page-wrap"></div></div>

如何从此类 HTML 中删除所有样式类 ID 等?

我有这样的正则表达式:

/<([a-z][a-z0-9]*)[^>]*?('/?)>/i

怎么了?如何在正则表达式的帮助下删除所有 HTML 属性?

这是小提琴:

http://jsfiddle.net/qL4maxn0/1/

首先,我建议您在这种情况下不要使用正则表达式,它们不是用来解析像 HTML 这样的树形结构的。

但是,如果您别无选择,我认为对于请求的问题,您可以使用正则表达式。

在我看来,你忘记了空格、口音等。您可以使用不允许将大于 > 和小于 < 符号的事实用作原始文本。

/<'s*([a-z][a-z0-9]*)'s.*?>/gi

并调用它:

result = body.replace(regex, '<$1>')

对于给定的样本,它会产生:

<title>Ololo - text’s life</title><div><div><div><div><div><div><div>olololo<ul><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li></ul><p>bbcvbcvbcvbcvbcvbcvbcvb</p></div></div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div></div></div>

你不应该在这里使用正则表达式。

var html = '<title>Ololo - text’s life</title><div class="page-wrap"><div class="ng-scope"><div class="modal custom article ng-scope in" id="new-article" aria-hidden="false" style="display: block;"><div class="modal-dialog first-modal-wrapper"><div class="modal-content"><div class="modal-body full long">                        <div class="form-group">olololo<ul style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);"><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li>                            </ul><p style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);">bbcvbcvbcvbcvbcvbcvbcvb</p></div><div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div class="page-wrap"></div></div>';
var div = document.createElement('div');
div.innerHTML = html;
function removeAllAttrs(element) {
    for (var i = element.attributes.length; i-- > 0;)
    element.removeAttributeNode(element.attributes[i]);
}
function removeAttributes(el) {
    var children = el.children;
    for (var i = 0; i < children.length; i++) {
        var child = children[i];
        removeAllAttrs(child);
        if (child.children.length) {
            removeAttributes(child);
        }
    }
}
removeAttributes(div);
console.log(div.innerHTML);

工作小提琴

您缺少使替换全局的g标志。

/<([a-z][a-z0-9]*)[^>]*?('/?)>/ig

此外,如果您出于安全目的这样做,请考虑使用适当的 HTML 清理器:在客户端清理/重写 HTML