使用JavaScript从URL中提取路径的正确方法是什么?
例子: 我有URL http://www.somedomain.com/account/search?filter=a#top 但我只想要这一份 /账户/搜索
我使用jQuery,如果有任何东西可以利用。
使用JavaScript从URL中提取路径的正确方法是什么?
例子: 我有URL http://www.somedomain.com/account/search?filter=a#top 但我只想要这一份 /账户/搜索
我使用jQuery,如果有任何东西可以利用。
当前回答
如果您想获取存储在变量中的URL的部分,我建议您使用URL- parse
const Url = require('url-parse');
const url = new Url('https://github.com/foo/bar');
根据文档内容,抽取了以下几个部分:
The returned url instance contains the following properties: protocol: The protocol scheme of the URL (e.g. http:). slashes: A boolean which indicates whether the protocol is followed by two forward slashes (//). auth: Authentication information portion (e.g. username:password). username: Username of basic authentication. password: Password of basic authentication. host: Host name with port number. hostname: Host name without port number. port: Optional port number. pathname: URL path. query: Parsed object containing query string, unless parsing is set to false. hash: The "fragment" portion of the URL including the pound-sign (#). href: The full URL. origin: The origin of the URL.
其他回答
有一个内置窗口的属性。Location对象,该对象将为当前窗口提供该对象。
// If URL is http://www.somedomain.com/account/search?filter=a#top
window.location.pathname // /account/search
// For reference:
window.location.host // www.somedomain.com (includes port if there is one)
window.location.hostname // www.somedomain.com
window.location.hash // #top
window.location.href // http://www.somedomain.com/account/search?filter=a#top
window.location.port // (empty string)
window.location.protocol // http:
window.location.search // ?filter=a
更新,对任何URL使用相同的属性:
这个模式被标准化为一个叫做URLUtils的接口,你猜怎么着?既有窗口。位置对象和锚元素实现了接口。
所以你可以对任何URL使用上面相同的属性-只需用URL创建一个锚并访问属性:
var el = document.createElement('a');
el.href = "http://www.somedomain.com/account/search?filter=a#top";
el.host // www.somedomain.com (includes port if there is one[1])
el.hostname // www.somedomain.com
el.hash // #top
el.href // http://www.somedomain.com/account/search?filter=a#top
el.pathname // /account/search
el.port // (port if there is one[1])
el.protocol // http:
el.search // ?filter=a
[1]:浏览器对包含port的属性的支持不一致,请参见http://jessepollak.me/chrome-was-wrong-ie-was-right
这在最新版本的Chrome和Firefox中都有效。我没有可供测试的Internet Explorer版本,所以请使用JSFiddle示例进行测试。
JSFiddle例子
还有一个即将到来的URL对象,它将为URL本身提供这种支持,而不需要锚元素。看起来目前还没有稳定的浏览器支持它,但据说它将在Firefox 26中推出。当你认为你可能支持它时,试试这里。
window.location.href.split('/');
将为您提供一个包含所有URL部分的数组,您可以像正常数组一样访问它。
或者@Dylan提出了一个更优雅的解决方案,只有路径部分:
window.location.pathname.split('/');
有一个很有用的Web API方法叫做URL
const url =新的url ('https://www.somedomain.com/account/search?filter=a#top'); console.log (url.pathname.split(“/”).slice (1));//删除前导斜杠 const params = new URLSearchParams(url.search) console.log(“过滤:“,params.get(“过滤器”))
如果这是当前url,使用window.location.pathname,否则使用正则表达式:
var reg = /.+?:\/\/.+?(\/.+?)(?:#|\?|$)/;
var pathname = reg.exec( 'http://www.somedomain.com/account/search?filter=a#top' )[1];
如果您想获取存储在变量中的URL的部分,我建议您使用URL- parse
const Url = require('url-parse');
const url = new Url('https://github.com/foo/bar');
根据文档内容,抽取了以下几个部分:
The returned url instance contains the following properties: protocol: The protocol scheme of the URL (e.g. http:). slashes: A boolean which indicates whether the protocol is followed by two forward slashes (//). auth: Authentication information portion (e.g. username:password). username: Username of basic authentication. password: Password of basic authentication. host: Host name with port number. hostname: Host name without port number. port: Optional port number. pathname: URL path. query: Parsed object containing query string, unless parsing is set to false. hash: The "fragment" portion of the URL including the pound-sign (#). href: The full URL. origin: The origin of the URL.