如何删除以字母+数字开头的HTML属性?

huangapple go评论55阅读模式
英文:

How to remove HTML attribute starting with letter + number?

问题

我想删除以字母 "z" 开头,后跟任何数字("z#")的属性标签。

但是当运行此代码段时,我遇到了错误,如您所见:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
    $("#main article").each(function(){
      var DE = new RegExp(/z{1}(\d{0,})\w+/); 

      $(this).removeAttr(function (){
        return (this.match(DE));
      });
    });


<!-- language: lang-html -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <section id="main">
      <article z1  > AAA </article>
      <article z2  > BBB </article>
      <article z3  > CCC </article>
      <article z101> DDD </article>
      <article z102> EEE </article>
      <article z103> FFF </article>
    </section>

<!-- end snippet -->

问题是什么?

英文:

I want to remove attribute tags that start with a letter "z" followed by any digits ("z#").

But I have errors as you can see when running this snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#main article&quot;).each(function(){
  var DE = new RegExp(/z{1}(\d{0,})\w+/); 

  $(this).removeAttr(function (){
    return (this.match(DE));
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;section id=&quot;main&quot;&gt;
  &lt;article z1  &gt; AAA &lt;/article&gt;
  &lt;article z2  &gt; BBB &lt;/article&gt;
  &lt;article z3  &gt; CCC &lt;/article&gt;
  &lt;article z101&gt; DDD &lt;/article&gt;
  &lt;article z102&gt; EEE &lt;/article&gt;
  &lt;article z103&gt; FFF &lt;/article&gt;
&lt;/section&gt;

<!-- end snippet -->

What is the problem?

答案1

得分: 1

以下是要翻译的内容:

您的尝试中存在一些问题:

  • removeAttr 不接受函数作为参数。您必须传递属性的名称(一个字符串)。
  • 没有对属性进行迭代。您可以使用 this.attributes 集合进行迭代。
  • \w+ 允许字母跟随在末尾。这与您在问题中描述的不一致。
  • 正则表达式缺少字符串结束标记,因此它也允许模式出现在长字符串的中间,其中包含其他字符。

另外:

  • 当您已经有正则表达式字面量时,不需要 RegExp 构造函数。
  • {1} 在正则表达式中是无用的。
  • {0,} 等效于 *
  • 要检查字符串是否与正则表达式匹配,不需要使用 match(它返回一个数组),而可以使用 test
$("#main article").each(function(i, article) {
    $(this.attributes).each(function () {
        if (/^z\d*$/.test(this.name)) $(article).removeAttr(this.name);
    });
});
console.log($("#main").html());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section id="main">
  <article z1> AAA </article>
  <article z2> BBB </article>
  <article z3> CCC </article>
  <article z101> DDD </article>
  <article z102> EEE </article>
  <article z103> FFF </article>
</section>

希望这有帮助。

英文:

There are a few issues in your attempt:

  • removeAttr doesn't take a function as argument. You have to pass the name of the attribute (a string).
  • There is no iteration happening over the attributes. For that you could use the this.attributes collection
  • The \w+ would allow letters to follow at the end. That's not what you stated in the question.
  • The regex lacks end-of-string markers, so it would also allow the pattern to occur somewhere in the middle of a long string with other characters.

Also:

  • You don't need the RegExp constructor when you already have a regex literal
  • {1} is useless in a regex.
  • {0,} is equivalent to *
  • To check whether a string matches with a regex, you don't need match (which returns an array), but can better use test.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#main article&quot;).each(function(i, article) {
    $(this.attributes).each(function () {
        if (/^z\d*$/.test(this.name)) $(article).removeAttr(this.name);
    });
});
console.log($(&quot;#main&quot;).html());

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;section id=&quot;main&quot;&gt;
  &lt;article z1  &gt; AAA &lt;/article&gt;
  &lt;article z2  &gt; BBB &lt;/article&gt;
  &lt;article z3  &gt; CCC &lt;/article&gt;
  &lt;article z101&gt; DDD &lt;/article&gt;
  &lt;article z102&gt; EEE &lt;/article&gt;
  &lt;article z103&gt; FFF &lt;/article&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 03:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定