【问题标题】:Regex to remove all attributes from nested html tags - Javascript正则表达式从嵌套的 html 标签中删除所有属性 - Javascript
【发布时间】:2021-12-22 09:15:06
【问题描述】:

我想使用正则表达式删除 html 标签的属性。它可以是任何 html 元素并允许嵌套元素,例如:

<div fadeout"="" style="margin:0px;" class="xyz">
    <img src="abc.jpg" alt="" />
    <p style="margin-bottom:10px;">
    The event is celebrating its 50th anniversary K&ouml;&nbsp;
    <a style="margin:0px;" href="http://www.germany.travel/">exhibition grounds in Cologne</a>.
    </p>
    <p style="padding:0px;"></p>
    <p style="color:black;">
       <strong>A festival for art lovers</strong>
    </p>
</div>

或者是这样的

&lt;span style="margin: 0;"&gt;&lt;p class="abc"&gt; Test text&lt;/p&gt;&lt;/span&gt;

因为安全原因,需要移除属性

我尝试删除的内容

s/(<\w+)\s+[^>]*/$1/

<*\b[^<]*>(?:[^<]+(?:<(?!\/?div\b)[^<]*)*|(?R))*<\/*>\s*
<([a-z][a-z0-9]*)[^>]*?(\/?)>

但不工作

【问题讨论】:

  • 你为什么要尝试使用正则表达式呢?属性不能用引号引起来,它们可以用单引号引起来。见stackoverflow.com/a/69902509/227299 任何试图在未来处理这个正则表达式的人都会讨厌你! ??????
  • 没有强制您删除所有属性的安全要求,您做错了事。您很可能会破坏预期的功能,或者仍然存在 XSS 漏洞。请参阅npmjs.com/package/sanitize-html 可以指导您。

标签: javascript html angular regex


【解决方案1】:

我建议你不要在这种情况下使用正则表达式,但如果你别无选择,也许你正在寻找这样的东西:

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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
【解决方案2】:

使用 DOM 的好处是您可以使用一整套专门为操作 DOM 设计的工具!然而,人们坚持将这种复杂的结构化数据格式视为只是一个愚蠢的字符串,并开始使用正则表达式对其进行破解。

为工作使用正确的工具。

function removeAttributesRecursively(el) {
  Array.from(el.attributes).forEach(function(attr) {
    // you'll probably want to include extra logic here to
    // preserve some attributes (a href, img src, etc)
    // instead of blindly removing all of them
    el.removeAttribute(attr.name);
  });
  // recurse:
  Array.from(el.children).forEach(function(child) {
    removeAttributesRecursively(child)
  })
}

const root = document.getElementById('input');

removeAttributesRecursively(root)

console.log(root.innerHTML)
<div id="input">
  <div fadeout="" style="margin:0px;" class="xyz">
    <img src="abc.jpg" alt="" />
    <p style="margin-bottom:10px;">
      The event is celebrating its 50th anniversary K&ouml;&nbsp;
      <a style="margin:0px;" href="http://www.germany.travel/">exhibition grounds in Cologne</a>.
    </p>
    <p style="padding:0px;"></p>
    <p style="color:black;">
      <strong>A festival for art lovers</strong>
    </p>
  </div>
</div>

【讨论】:

  • 我正要添加一条评论,要求您删除极难阅读的Array.prototype.slice.apply 电话:D
  • 老派,对不起。更新为使用 ES6 方法:)
  • @Spectric 的方法还是更好;我应该考虑首先选择每个元素而不是递归
【解决方案3】:

Regex should not be used to parse HTML.

相反,您应该使用DOMParser 来解析字符串,遍历每个元素的属性并使用Element.removeAttribute:

const str = `<div fadeout"="" style="margin:0px;" class="xyz">
    <img src="abc.jpg" alt="" />
    <p style="margin-bottom:10px;">
    The event is celebrating its 50th anniversary K&ouml;&nbsp;
    <a style="margin:0px;" href="http://www.germany.travel/">exhibition grounds in Cologne</a>.
    </p>
    <p style="padding:0px;"></p>
    <p style="color:black;">
       <strong>A festival for art lovers</strong>
    </p>
</div>`

function stripAttributes(html){
  const parsed = new DOMParser().parseFromString(html, 'text/html')
  parsed.body.querySelectorAll('*').forEach(elem => [...elem.attributes].forEach(attr => elem.removeAttribute(attr.name)))
  return parsed.body.innerHTML;
}

console.log(stripAttributes(str))

【讨论】:

  • 致 OP:要小心,因为删除所有属性很可能会出现问题。为什么你会有一个&lt;img&gt; 而没有一个src。为什么&lt;a&gt; 没有href 或至少没有名字?我不会尝试自己清理 HTML。使用像 npmjs.com/package/sanitize-html 这样的库
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 2011-07-27
  • 2017-08-16
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
相关资源
最近更新 更多