有什么方法可以使用 JavaScript 从 URL 中提取完整路径的一部分?
示例:我有 URL http://www.somedomain.com/account/6099bb4e2eb24bab85e8b76851a14260/search?filter=a#top
但我只想得到这部分 6099bb4e2eb24bab85e8b76851a14260
这是一个随机 id 几个例子,如:
6099bb4e2eb24bab85e8b76851a14260,
93b04efd1a734407b28f95d9c5278c0f,
94c9483fdb3747b38e4836fe9294cbfe,
93b04efd1a734407b28f95d9c5278c0f
请帮帮我...它让我发疯。学习....
你可以像这样去做,
const url=new URL('http://www.somedomain.com/account/6099bb4e2eb24bab85e8b76851a14260/search?filter=a#top');
/*
url.pathname="/account/6099bb4e2eb24bab85e8b76851a14260/search"
url.pathname.split('/')=["","account","6099bb4e2eb24bab85e8b76851a14260","search"]
*/
const accountId=url.pathname.split('/')[2];
console.log(accountId);
有关如何URL
在 JS 中使用对象的更多信息,请参阅此内容,https://developer.mozilla.org/en-US/docs/Web/API/URL
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句