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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:32:57  来源:igfitidea点击:

Regex to find id in url

javascriptregexmatch

提问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 /productin the url just not at the start of the path.

唯一永远相同的是/product。但是我还有一些其他页面,其中/producturl 中也有,只是不在路径的开头。

What would the regex look like?

正则表达式会是什么样子?

回答by Matt

  1. Use window.location.pathnameto retrieve the current path (excluding TLD).

  2. Use the JavaScript string matchmethod.

  3. Use the regex /^\/product\/(\d+)/to find a path which starts with /product/, then one or more digits (add iright at the end to support case insensitivity).

  4. 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.
    }
    
  1. 使用window.location.pathname检索(不含TLD)的电流通路。

  2. 使用 JavaScript 字符串 match方法。

  3. 使用正则表达式/^\/product\/(\d+)/查找以 /product/ 开头的路径,然后是一位或多位数字(i在末尾添加以支持不区分大小写)。

  4. 想出这样的事情:

    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 gmodifier:

要获得第一个结果,您可以删除g修饰符:

 .match(/\/(\d+)+[\/]?/)

var url = 'http://localhost:4000/#/trees/8';


var id = url.match(/\/(\d+)+[\/]?/);
//With and without slashes
console.log(id);
没有斜杠的 id 将在第二个元素中,因为这是在完整匹配中找到的第一个组。

Hope this helps people. Cheers!

希望这可以帮助人们。干杯!