gpt4 book ai didi

javascript - 如何正确拆分 url,然后在 javascript 中获取剩余部分?

转载 作者:行者123 更新时间:2023-12-01 00:52:49 24 4
gpt4 key购买 nike

我有一个来自 WordPress 的 json 对象中的 url 列表。我想用一个前缀来分割它们,我可以看到它是 https://www.example.co 然后获取其余部分,例如 /cat1/aa/bb/cc,/? page_id=8830,/xmlrpc.php

var myUrl = [
{
"URL": "https://www.example.co/cat1/aa/bb/cc",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat2/aa",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat1/aa/bb/cc/dd/ee",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat3/aa/bb/cc/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat2/aa/bb",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat1/aa/bb",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/news/why-residents/attachment/san-francisco-blog/feed/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/xmlrpc.php",
"Last crawled": "Jun 19, 2019"
},
{
"URL": "https://www.example.co/?page_id=8830",
"Last crawled": "Jun 18, 2019"
},
]

我当前的代码如下所示:

myUrl.forEach(function(item) {
var urlList = item.URL.split("/");
//console.log(urlList);
console.log("https://www.example.co/" + urlList[3]);
});

上面的代码目前无法从前缀中获取剩余部分。

非常感谢任何帮助。谢谢

最佳答案

如果您只想从 URL 中删除域,则可以使用 String.prototype.replace 而不是 String.prototype.split

例如:

const myUrl = [
{
"URL": "https://www.example.co/cat1/aa/bb/cc",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat2/aa",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat1/aa/bb/cc/dd/ee",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat3/aa/bb/cc/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat2/aa/bb",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/cat1/aa/bb",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/news/why-residents/attachment/san-francisco-blog/feed/",
"Last crawled": "Jun 23, 2019"
},
{
"URL": "https://www.example.co/xmlrpc.php",
"Last crawled": "Jun 19, 2019"
},
{
"URL": "https://www.example.co/?page_id=8830",
"Last crawled": "Jun 18, 2019"
},
];

const paths = myUrl.map(value => value.URL.replace('https://www.example.co', ''));

console.log(paths);

关于javascript - 如何正确拆分 url,然后在 javascript 中获取剩余部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56844548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com