Javascript 正则表达式仅从 URL 获取页码

Javascript regex get only page number from URL

我有URL喜欢 https://example.com/my-list/page/35/?a=45&b=56

我想在 url 中使用 /page/ 之后的正则表达式获取页码, 我想要的结果是 35

使用 RegEx 有 2 种简单的方法:

第一种方式:

var s = "https://example.com/my-list/page/35/?a=45&b=56";
//  First way uses capture groups based on what comes before the page number: "/page/"
s.match(/\/page\/(\d*)/)[1]; // "35"

第二种方式:

//  The second involves using capture grouping based on the search param that appears to follow.
//  However, this way might not be ideal as you may extend the URI and make a major change that would then require an edit to this. Of course, that could happen with the previous as well.

s.match(/(\d*)(?=\/\?)/)[0]  //  "35"

您也可以 split 字符串,然后使用正则表达式来抓取字符串的开头:

s.split("/page/")[1].match(/^\d*/);  //  "35"

您也可以根据最常见的元素“/”简单地拆分整个字符串。如果您的 URI 模式始终相同,那么这可能是最简单易读的解决方案之一。但是,如果 URI 模式更改为包含更多字段,那么您可能需要调整索引号。

s.split("/")[5]  //  "35"

您也可以根据 "page" 的索引使用 substr。问题是页码可能是 1 位、2 位或更多:

s.substr(s.indexOf("page/")+5, 2)  //  "35"
//  +5 due to length of phrase i use

您可以使用 substrindex 获取您想要的字符串,将页码放在字符串的开头。然后使用正则表达式提取数字并将其置于 0 索引处。如果出于某种原因没有页码,这可能更实用。例如,用户最终到达 "page" 索引基数。

s.substr(s.indexOf("page/")+5).match(/^\d*/)[0]  //  35

WordPress 还可以使用另一种 url 格式,其中页码存储在 url 查询 paged 参数中,因此最安全的解决方案是涵盖两种 url 格式。另一个问题是,在任何档案的第一页上,您都不会在 url 中找到页码,因此我们也需要涵盖这种情况。

这是适用于所有情况的简单函数:

function getPageFromUrl( url ) {
    const match = url.match(/(?:\/page\/|[?&]paged=)([1-9]\d*)/)
    if ( match ) {
        return Number(match[1]);
    }
    return 1;
}