如何在 JavaScript 中加入相对 URL
我想连接两个字符串,每个字符串代表 Javascript 中的相对 URL。
我想使用以下示例加入基本 URL http://www.adress.com/more/evenmore
:
../../adress
(以及预期的输出:http://www.adress.com/adress
)../adress
(具有预期输出http://www.adress.com/更多/地址
)
最好的方法是什么?我正在考虑使用正则表达式并检查
相对 URL 前面有多少个 ../
,然后从基本 URL 中减去该数量并将它们添加到剩下的内容中。
I want to join two strings each representing a relative URL in Javascript.
I want to join the base URL http://www.adress.com/more/evenmore
with the following examples:
../../adress
(with the expected output:http://www.adress.com/adress
)../adress
(with the expected outputhttp://www.adress.com/more/adress
)
What would be the best way? I was thinking of using regexp and checking
how many ../
preceed the relative URL, then subtracting that amount from the baseurl and adding them to what is left.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
8 年后,许多浏览器(Internet Explorer 除外)都支持 URL构造函数 (
URL(url [, base])
)。8 years later, many browsers (except for Internet Explorer) support the URL constructor (
URL(url [, base])
).以下函数分解 URL,然后解析它。
The following function decomposes the URL then resolves it.
使用 URI.js (urijs - npm):
absoluteTo()
:Using URI.js (urijs - npm):
absoluteTo()
:@ning 提到的 ECMAScript
URL
Web API 是一个很好的起点:特别是因为它可以在普通 JS 实现(Node 等)中使用,并且不需要您可以使用一个库来完成 nowq 实现已经完成的事情。请参阅 MDN 文档,了解更多具体示例 ,是一个很好的起点。直接借用(某种程度上)他们的文档:
如果您要执行上述操作,则
console.log
的 .toString 构造的URL
对象,您的输出将是:'https://developer.mozilla。 org/en-US/docs'
。重要的是,如果您查阅文档的语法部分,您会注意到 第二 参数是可选的,并且 第二 参数(如上面的示例所示)代表 < em>base URL(尽管仅在提供两个参数的情况下)。
如果两个参数值都是绝对 URL,Web API 将采用第一个并丢弃第二个。
如果您正在使用 Node.js,我建议您查看本机 path 模块在相对路径上工作,再次使用库。这里的主要动机是,旋转你自己的算法(可能只是在这里或那里调用
path
)可能比引入一个库更好,该库将引入几个其他依赖项,这些依赖项可能会引入漏洞和不必要的膨胀适合您的应用程序(或者对于您的需要来说太重)。但是,如果您在前端工作,则不会有可用的
path
- 正如 @cburgmer 的回答评论 - Web API 的URL
不支持提到的相对路径情况。在这种情况下,您可能需要寻找一个库来为您完成此任务;然而,再次考虑到其他答案,我会考虑尝试一种家庭方法。值得赞扬的是,
URI.js
目前仅集成一个非开发人员。依赖并且占用空间不大。The ECMAScript
URL
Web API mentioned by @ning is a good place to start: especially as it is available in vanilla JS implementations (Node, etc.) and doesn't require you to use a library that does something the implementation nowq already accomplishes. Consulting the MDN documentation, more specifically the examples, is a great place to start.Borrowing (somewhat) directly from their documentation:
If you were to do the above, then
console.log
the .toString of the constructedURL
object, your output would be:'https://developer.mozilla.org/en-US/docs'
.Importantly, if you consult the Syntax section of the documentation, you will note that the second argument is optional and the second argument (as seen in the above example) represents the base URL (though only in the case of two arguments being supplied).
If both argument values are absolute URLs, Web API honors the first and discards the second.
If you are working with Node.js, I would encourage you to look at the native path module for doing work on relative paths, again over using a library. The main motivation here is that spinning up your own algorithm (probably just a call to
path
here and there) is potentially better than introducing a library that will pull in several other dependencies that may introduce vulnerabilities and unnecessary bloat to your application (or just be too heavy weight for what you need).However, if you are working on the front end, you won't have
path
available to you and - as mentioned in @cburgmer's answer comments - Web API'sURL
doesn't support the relative path case mentioned. In this case, you may need to look for a library to accomplish this for you; however, again, given the other answers, I'd consider trying out a home-spun approach.To their credit,
URI.js
currently only integrates one non-dev. dependency and doesn't have a huge footprint.