Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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正则表达式中从该url中获取数字_Javascript_Regex - Fatal编程技术网

如何在javascript正则表达式中从该url中获取数字

如何在javascript正则表达式中从该url中获取数字,javascript,regex,Javascript,Regex,我有这个网址 http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp?userID=1413795052&startIndex=0&endIndex=-1&filterBy=all 我想在javascript中使用正则表达式获取1413795052number,如何实现这一点 var url = 'http://nikerunning.nike.com/nikeplus/v2/services/

我有这个网址

http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp?userID=1413795052&startIndex=0&endIndex=-1&filterBy=all
我想在javascript中使用正则表达式获取
1413795052
number,如何实现这一点

var url = 'http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp?userID=1413795052&startIndex=0&endIndex=-1&filterBy=all';
var match = url.match(/userID=(\d+)/)
if (match) {
    var userID = match[1];
}
这与URL中userID参数的值相匹配

/userID=(\d+)/
是一个正则表达式文本。工作原理:

  • /
    是分隔符,如字符串的
  • userID=
    url
  • (\d+)
    搜索一个或多个十进制数字并捕获它(返回它)
试试:

var input = "http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp?userID=1413795052&startIndex=0&endIndex=-1&filterBy=all";

var id = parseInt( input.match(/userID=(\d+)/)[1] );

这将获取查询字符串中的所有数字:

window.location.search.match(/[0-9]+/);

请在stackoverflow中尝试:

window.location.pathname.match(/questions\/(\d+)/)[1]
> "7331140"
或作为整数:

~~window.location.pathname.match(/questions\/(\d+)/)[1]
> 7331140

谢谢,它成功了。。你能给我解释一下
/userID=(\d+)/
做了什么吗?那么为什么它只得到编号
1413795052
,而不得到
userID=1413795052
?整个匹配的字符串在match[0]中返回,所有捕获的字符串(括号下的部分)在match[x]中返回其中x是捕获组的编号