英文:
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 -->
$("#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 -->
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:
removeAttrdoesn'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.attributescollection - 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 
RegExpconstructor 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 usetest. 
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("#main article").each(function(i, article) {
    $(this.attributes).each(function () {
        if (/^z\d*$/.test(this.name)) $(article).removeAttr(this.name);
    });
});
console.log($("#main").html());
<!-- 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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论