Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Regex - Fatal编程技术网

Javascript 简单正则表达式模式,提取数字

Javascript 简单正则表达式模式,提取数字,javascript,regex,Javascript,Regex,我想从如下字符串的方括号中提取数字: "Item5Line[14].Id" 到目前为止,我所做的会导致Javascript中出现错误: index = Id.attr('name').match(/\[\d\d?\d?\]); 我对正则表达式非常陌生,所以请温柔一点:) 提前谢谢 看起来您缺少正则表达式中的结束符/ index = Id.attr('name').match(/\[\d\d?\d?\]/);

我想从如下字符串的方括号中提取数字:

"Item5Line[14].Id"
到目前为止,我所做的会导致Javascript中出现错误:

index = Id.attr('name').match(/\[\d\d?\d?\]);
我对正则表达式非常陌生,所以请温柔一点:)


提前谢谢

看起来您缺少正则表达式中的结束符
/

index = Id.attr('name').match(/\[\d\d?\d?\]/);
                                           ^ need this closing /
工作样本:


另外,@Josh M.有一个更好的正则表达式。

看起来您缺少正则表达式中的结束符
/

index = Id.attr('name').match(/\[\d\d?\d?\]/);
                                           ^ need this closing /
工作样本:

另外,@Josh M.有一个更好的正则表达式。

试试:
index=Id.attr('name').match(/\[(\d+)\]/)

然后可以从索引1中取出匹配项。

尝试:
index=Id.attr('name').match(/\[(\d+)\]/)

index = Id.attr('name').match(/\[\d\d?\d?\]);
然后你可以在索引1处退出比赛

index = Id.attr('name').match(/\[\d\d?\d?\]);
您忘了在末尾添加/了

你忘了在结尾添加/了。

谢谢你,Josh。我(和下面提到的每个人一样)错过了结尾“/”。这个表达式当然更简洁。我感谢你的帮助!涵盖regexp。您可能还想查看字符串对象(match())。如果您没有意识到()的用法,那么当您获得“index”值时,它实际上是一个数组。数组的元素按左括号的顺序匹配嵌入正则表达式中的括号对。因此,索引[0]以字符串的形式给出完整匹配,索引[1]给出由括号内捕获的部分组成的字符串(即,“14”)。尝试url:javascript:index=/hell(owo)rld/.exec(“helloworld”);警报(索引[1]);谢谢你,乔希。我(和下面提到的每个人一样)错过了结尾“/”。这个表达式当然更简洁。我感谢你的帮助!涵盖regexp。您可能还想查看字符串对象(match())。如果您没有意识到()的用法,那么当您获得“index”值时,它实际上是一个数组。数组的元素按左括号的顺序匹配嵌入正则表达式中的括号对。因此,索引[0]以字符串的形式给出完整匹配,索引[1]给出由括号内捕获的部分组成的字符串(即,“14”)。尝试url:javascript:index=/hell(owo)rld/.exec(“helloworld”);警报(索引[1]);