Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript正则表达式_Javascript_Jquery_Regex - Fatal编程技术网

javascript正则表达式

javascript正则表达式,javascript,jquery,regex,Javascript,Jquery,Regex,我想使用jQuery重命名元素的id属性。我需要匹配以下内容: 第1排 第2排 第xx排等 这就是我到目前为止所做的: $('.form .newsCategories .row .itemWrap .itemTop .inputBtn').each(function(index){ $(this).attr("id", $(this).attr("id").replace($(this).attr("id").match(/\[row_[0-9]+\]/), "roww_"+ind

我想使用jQuery重命名元素的id属性。我需要匹配以下内容:

  • 第1排
  • 第2排
  • 第xx排等
这就是我到目前为止所做的:

$('.form .newsCategories .row .itemWrap .itemTop .inputBtn').each(function(index){
    $(this).attr("id", $(this).attr("id").replace($(this).attr("id").match(/\[row_[0-9]+\]/), "roww_"+index));
    });
但这失败了。我想我的前女友有问题。请帮忙
谢谢

如果我了解您想要的结果ID,您可以这样做:

this.id = "row" + index;
例如,如果要查找所有这些文件并按文档顺序重新编号:

$("*[id^=row]").each(function(index) {
    this.id = "row" + index;
});
它使用(
^=
)只查找其
id
以“行”开头的元素,然后按文档顺序对其重新编号



主题外:请注意,根本没有理由使用
$(this).attr(“id”)
;DOM元素本身有一个
id
属性,它直接反映了
id
属性。

如果我了解您想要的结果id,您可以这样做:

this.id = "row" + index;
例如,如果要查找所有这些文件并按文档顺序重新编号:

$("*[id^=row]").each(function(index) {
    this.id = "row" + index;
});
它使用(
^=
)只查找其
id
以“行”开头的元素,然后按文档顺序对其重新编号



主题外:请注意,根本没有理由使用
$(this).attr(“id”)
;DOM元素本身有一个
id
属性,它直接反映
id
属性。

您有一组额外的方括号([])。将其更改为:

/row_[0-9]+/
或同等产品:

/row_\d+/

您有一组额外的方括号([])。将其更改为:

/row_[0-9]+/
或同等产品:

/row_\d+/

使用
[]
s创建一个您不需要执行的字符类。试试这个正则表达式


/row\u0-9]+/
使用
[]
创建一个您不需要执行的字符类。试试这个正则表达式


/row[0-9]+/

+1是的,OP似乎不必要地用attr(“id”)做了很多事。+1是的,OP似乎不必要地用attr(“id”)做了很多事。@Shane Km我会看看TJ Crowder的答案,您似乎做得太多了,无法简单地重命名
id
attribute@Shane我想看看TJ Crowder的答案,您似乎做了太多的工作,无法简单地重命名
id
属性