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

Javascript 使用正则表达式提取数字

Javascript 使用正则表达式提取数字,javascript,regex,match,Javascript,Regex,Match,我有一个字符串: name:demo;morestuff.nbvideo:3;morestuff_here:45 我需要从中提取视频编号。我用两个正则表达式来管理它,但我确信它可以在一个正则表达式中完成 以下是我现在拥有的: // get the nbvideo:XX part videoPart = sink.tag.match(/nbvideo:([0-9]+)/gi); // get the number from the video part videoCount = videoPar

我有一个字符串:

name:demo;morestuff.nbvideo:3;morestuff_here:45
我需要从中提取视频编号。我用两个正则表达式来管理它,但我确信它可以在一个正则表达式中完成

以下是我现在拥有的:

// get the nbvideo:XX part
videoPart = sink.tag.match(/nbvideo:([0-9]+)/gi);
// get the number from the video part
videoCount = videoPart[0].match(/([0-9]+)/gi)[0];

如何用一个正则表达式提取“nbvideo:”后面的数字?

从修饰符中删除
g
,然后访问第一个捕获组值,如下所示:

var sink_tag=“name:demo;morestuff.nbvideo:3;morestuff_here:45”; var m=sink_tag.match(/nbvideo:([0-9]+)/i); 如果(m){ videoPart=m[1]; document.body.innerHTML=videoPart;//演示
}只需在第一个正则表达式中使用捕获组。从修饰符中删除
g
,并通过索引1访问它
sink.tag.match(/nbvideo:([0-9]+)/i)[1]
(当然添加错误检查)谢谢@WiktorStribiżew。。。。工作就像一个没有帮助的咒语。如果您使用的是索引,则可以使用字符串按
.nbvideo:
拆分,并使用索引获取第一个字符。但这个数字是否只有一位数才是关键。