将网址分成几个部分,轻松完成其余部分

时间:2017-11-24 22:56:38

标签: javascript node.js

如果网址为https://example.com/web/fundamentals/course/dashboard

我可以将它拆分为数组。



const url = "example.com/web/fundamentals/course/dashboard"
path = url.split("/");
const [head1,head2, ...rest] = path;

console.log(head1);
console.log(head2);
console.log(rest);




但是如何将其余部分作为字符串fundamentals/course/dashboard,而不是没有" /"在里面。

2 个答案:

答案 0 :(得分:2)

你可以通过join加入数组元素。

rest.join('/')

更多信息here

答案 1 :(得分:1)

const url = "example.com/web/fundamentals/course/dashboard"
path = url.split("/");
const [head1,head2, ...rest] = path;

console.log(head1);
console.log(head2);
console.log(rest.join('/'));