javascript - 在 JavaScript 中缩小 URL
问题描述
我们如何在 JavaScript 中缩小 URL:
例如,如果链接是这样的,则不应在 .com 之后扩展
预期输出:
https://stackoverflow.com
谢谢你的时间,
解决方案:我使用 Regexp 来缩小 url
const yourUrl = {your url goes here}
const reConstructUrl = new RegExp(
[
"^(https?:)//", // protocol
"(([^:/?#]*)(?::([0-9]+))?)", // host (hostname and port)
'(/{0,1}[^?#]*)', // pathname
'(\\?[^#]*|)', // search
'(#.*|)$' // hash
].join("")
)
const matchedUrl = yourUrl.match(reConstructUrl);
现在您可以从 URL 中仅显示您想要的部分
解决方案
基于这个答案,你可以做这样的事情
const url = new URL('https://stackoverflow.com/questions/ask');
console.log(url.origin)
推荐阅读
- typo3 - 如何使用掩码请求重新加载 TYPO3 扩展字段
- r - R转换列表:值到名称和名称到值
- python - 尝试更改 xticklabels 时,pandas/matplotlib 中的 x 轴消失
- asp.net - (.NET Core 3.1+) 需要使用/调用 API 用户名和密码来访问 API 方法。调用/存储此数据的最佳和最安全的方式?
- python - 刚开始使用python,你可以看看这个吗?它返回一个错误......说':'预期
- android - 为 ViewGroup 发光
- python-3.x - 如何在bs4中选择一个数字的id?
- soapui - SOAPUI testrunner 从命令行测试属性
- amazon-web-services - 是否可以使用 Ansible 清除 AWS 队列
- ios - 覆盖 UICollectionViewCompositionalLayout 中的 LayoutAttributesForElements 以创建弹性标题?