【发布时间】:2020-04-18 18:06:57
【问题描述】:
如果输入为https://example.com/page.html?query=string,我如何使用JavaScript返回字符串example.com?
现在,如果我使用 window.location.hostname – 或 location.hostname – 返回值是example.com/,但我想要example.com,没有尾部斜杠。
这个问题类似于@r1853 的“How to remove trailing slash from window.location.pathname”,但是因为 JavaScript 提供了许多方法来获取 URI 的一部分(location.hostname、location.host、location.href),并且因为格式正确的 URI是一个封闭的类,我认为有一个比使用正则表达式从它们中删除尾部斜杠更便宜的选择。
【问题讨论】:
标签: javascript string data-manipulation window.location trailing-slash