javascript - 移除端口并保留主机
问题描述
我有如下网址
https://www.aaaa.com:5000 -> https://www.aaaa.com
https://bbbb.com:443 -> https://bbbb.com
https://cccc.com -> https://cccc.com
我只需要删除端口..
我尝试了以下不起作用的方法,它需要所有数据
https://regex101.com/r/CIiALR/1
(https?://.*):(\d*)\/?(.*)`
诀窍是我必须只使用正则表达式而不是 js lib,因为我需要它来使用 Vector。
https://vector.dev/docs/reference/vrl/
另外:https ://vector.dev/docs/reference/vrl/#parse-custom-logs
解决方案
不需要正则表达式,您可以将url
对象用于此类工作。
var url = new URL('https://www.aaaa.com:5000');
url.port = '';
console.log(url.toString());
有关 url 对象的更多信息 - https://developer.mozilla.org/en-US/docs/Web/API/URL
推荐阅读
- javascript - 从移动浏览器 URL 调用 Facebook 摄像头
- python - 我如何确定这个基于文本的游戏的获胜者?
- office365 - 在 SharePoint 中隐藏个人机密
- php - PHP循环遍历列表并存储在MYSQL数据库中
- c++ - 如何在 Windows 中使用 c++ 将套接字分配给特定的网络接口?
- java - 我如何在 json 文件中获取每个 json elemet 并将它们放入数组中
- java - 无法使用 java 使用 Backup.execute 备份我的 h2 数据库
- sql-server-2014 - SQL Server 数据库是否需要备份设备来备份数据库?
- r - 我想将当前数据框(带有一些空单元格)重塑为另一个数据框并创建新列(连接)
- google-sheets - 如何缩短这个公式?