javascript 正则表达式在 url 中查找 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6117501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to find id in url
提问by PeeHaa
I have the following URL:
我有以下网址:
http://example.com/product/1/something/another-thing
http://example.com/product/1/something/another-thing
Although it can also be:
虽然它也可以是:
http://test.example.com/product/1/something/another-thing
http://test.example.com/product/1/something/another-thing
or
或者
http://completelydifferentdomain.tdl/product/1/something/another-thing
http://completelydifferentdomain.tdl/product/1/something/another-thing
And I want to get the number 1 (id) from the URL using Javascript.
我想使用 Javascript 从 URL 中获取数字 1 (id)。
The only thing that would always be the same is /product
. But I have some other pages where there is also /product
in the url just not at the start of the path.
唯一永远相同的是/product
。但是我还有一些其他页面,其中/product
url 中也有,只是不在路径的开头。
What would the regex look like?
正则表达式会是什么样子?
回答by Matt
Use
window.location.pathname
to retrieve the current path (excluding TLD).Use the JavaScript string
match
method.Use the regex
/^\/product\/(\d+)/
to find a path which starts with /product/, then one or more digits (addi
right at the end to support case insensitivity).Come up with something like this:
var res = window.location.pathname.match(/^\/product\/(\d+)/); if (res.length == 2) { // use res[1] to get the id. }
使用
window.location.pathname
检索(不含TLD)的电流通路。使用 JavaScript 字符串
match
方法。使用正则表达式
/^\/product\/(\d+)/
查找以 /product/ 开头的路径,然后是一位或多位数字(i
在末尾添加以支持不区分大小写)。想出这样的事情:
var res = window.location.pathname.match(/^\/product\/(\d+)/); if (res.length == 2) { // use res[1] to get the id. }
回答by rid
/\/product\/(\d+)/
and obtain $1
.
/\/product\/(\d+)/
并获得$1
.
回答by Thomas Shields
Just, as an alternative, to do this without Regex (though i admit regex is awfully nice here)
只是,作为替代方案,在没有正则表达式的情况下执行此操作(尽管我承认正则表达式在这里非常好)
var url = "http://test.example.com//mypage/1/test/test//test";
var newurl = url.replace("http://","").split("/");
for(i=0;i<newurl.length;i++) {
if(newurl[i] == "") {
newurl.splice(i,1); //this for loop takes care of situatiosn where there may be a // or /// instead of a /
}
}
alert(newurl[2]); //returns 1
回答by Nebulosar
I would like to suggest another option.
我想建议另一种选择。
.match(/\/(\d+)+[\/]?/g)
This would return all matches of id's present.
这将返回 id 存在的所有匹配项。
Example:
例子:
var url = 'http://localhost:4000/#/trees/8/detail/3';
// with slashes
var ids = url.match(/\/(\d+)+[\/]?/g);
console.log(ids);
//without slashes
ids = url.match(/\/(\d+)+[\/]?/g).map(id => id.replace(/\//g, ''));
console.log(ids);
This way, your URL doesn't even matter, it justs retrieves all parts that are number only.
这样,您的 URL 甚至无关紧要,它只是检索所有仅包含数字的部分。
To just get the first result you could remove the g
modifier:
要获得第一个结果,您可以删除g
修饰符:
.match(/\/(\d+)+[\/]?/)
var url = 'http://localhost:4000/#/trees/8';
var id = url.match(/\/(\d+)+[\/]?/);
//With and without slashes
console.log(id);
Hope this helps people. Cheers!
希望这可以帮助人们。干杯!