【问题标题】:Remove all html attributes with regex (replace)使用正则表达式删除所有 html 属性(替换)
【发布时间】:2015-05-10 16:21:21
【问题描述】:

例如我有这样的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/

【问题讨论】:

  • Don't try to parse HTML with regexs。 (除非非常有限,已知html)
  • @JamesThorpe 没有别的办法(((
  • @jamesthorpe 说的是对的。如果您想这样做,请编辑您的 html 或 - 在最坏的情况下,如果您必须 - 使用 javascript 删除每个...
  • 总有另一种方式=D
  • @bwegs 属性不是标签

标签: javascript html regex


【解决方案1】:

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

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);

Working Fiddle

Source

【讨论】:

  • 我建议使用documentFragment 而不是div,但这只是一个实现细节。
  • @zzzzBov 感谢您提供的信息(我不知道这一点)。如果我这样做,不知何故我无法得到结果。
  • 您不能只添加document fragments 来替换div 元素,但其想法是div 将上下文添加为body 元素,其中文档片段位于上下文无关,可以代表 任何 HTML 片段。
  • 我认为使用div 是XSS 的向量,例如如果有人尝试&lt;img src="invalidurl" onerror="alert('xss');"&gt;。假设由于文档片段是上下文无关的,它不会在您将 html 添加到其中后立即尝试加载图像(并因此触发 JS)?
【解决方案2】:

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

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

在我看来您忘记了空格、重音符号等。您可以使用大于 &gt; 和小于 &lt; 的符号不允许作为原始文本的事实。

/<\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>

【讨论】:

  • @brabertaser1992:修改...更好?
  • @brabertaser1992:虽然我想知道在开始标签&lt;和标签名称之间写一个空格是否是有效的html......
  • 确保 nov 有效)但可能用户无效并犯了一些错误)
  • 最后一个问题:很好,只是如何将 src 用于图像或将 href 用于标签等?只有href和src?谢谢,是的,我知道:正则表达式对我来说是最糟糕的部分)
  • @brabertaser1992:在那种情况下,我真的不会使用正则表达式。有没有办法可以使用 DOM 解析器或任何东西?几乎所有的正则表达式都会有缺点并最终在这种替换上失败......
【解决方案3】:

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

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

此外,如果您出于安全目的这样做,请考虑使用适当的 HTML sanitizer:Sanitize/Rewrite HTML on the Client Side

【讨论】:

  • regex101.com/r/wR5wC4/1 - 所以它没有获得所有类样式等,例如多个类......我做错了什么?
  • @brabertaser1992,是的。我指出了为什么您的原始正则表达式不适用于多个标签。但还有其他问题。例如,&lt;img title="2&gt;1 onerror=alert(42) ""&gt;"&gt; 将拆分。请参阅上面的 cmets 以尝试在不先阅读语言规范的情况下使用正则表达式解析 HTML。
猜你喜欢
  • 2017-07-15
  • 1970-01-01
  • 2020-12-04
  • 2015-07-06
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多