Home > database >  Remove port and keep the host
Remove port and keep the host

Time:11-02

I've URL like following

https://www.aaaa.com:5000  -> https://www.aaaa.com
https://bbbb.com:443   -> https://bbbb.com
https://cccc.com     -> https://cccc.com

I need to remove only the port ..

I've tried with the following which doesn't works, it takes all the data

https://regex101.com/r/CIiALR/1

(https?://.*):(\d*)\/?(.*)`

The trick is that I must use only regex not js lib, as I need it for using Vector.

https://vector.dev/docs/reference/vrl/

Also: https://vector.dev/docs/reference/vrl/#parse-custom-logs

CodePudding user response:

No need for regex, you may use the url object for this kind of work.

var url = new URL('https://www.aaaa.com:5000');
url.port = '';
console.log(url.toString());
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

More about the url object - https://developer.mozilla.org/en-US/docs/Web/API/URL

CodePudding user response:

Looking at https://vector.dev/docs/reference/vrl/ you can use a named capture group and optionally match the port number:

^(?P<withoutport>https?://[^/\s] ?)(?::\d |$)
  • ^ Start of string
  • (?P<withoutport> Named group
    • https?:// Match the protocol
    • [^/\s] ? Match by any char except / or a whitespace char in a non greedy way
  • ) Close named group
  • (?::\d |$) Match : and 1 digits, or assert the end of the string

Regex demo

Or you can make it as specific as you require:

 ^(?P<withoutport>https?://[^/\s] ?)(?:[:?#/]\S*)?$

Regex demo

  • Related