首页 > 解决方案 > 移除端口并保留主机

问题描述

我有如下网址

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

标签: javascriptregex

解决方案


不需要正则表达式,您可以将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


推荐阅读