如何在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是捕获组的编号