Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting port from URL string using Javascript [duplicate]

I would like a function in javascript that will get as a parameter an url and will return the port of that URL as it follows:

  • If there's a http or https (port 80 / 443) it won't be shown in url structure but I want them returned anyway.
  • If there's another port, I want that to be returned.

Example:

function myFunction(url){
    something here
    ...
    return port
}

I've seen that this can be done easy using some additional libraries but I do not want to use one. I didn't work with js since now and I would really appreciate if somebody will also be able to explain his solution.

like image 673
Cajuu' Avatar asked Dec 04 '25 10:12

Cajuu'


2 Answers

From what I get, you don't want to use location as the URL to subtract the port from, just any string as an URL. Well, I came up with this, for such a case. This function takes any string (but you can pass it the location URL anyway, and it works the same):

function getPort(url) {
    url = url.match(/^(([a-z]+:)?(\/\/)?[^\/]+).*$/)[1] || url;
    var parts = url.split(':'),
        port = parseInt(parts[parts.length - 1], 10);
    if(parts[0] === 'http' && (isNaN(port) || parts.length < 3)) {
        return 80;
    }
    if(parts[0] === 'https' && (isNaN(port) || parts.length < 3)) {
        return 443;
    }
    if(parts.length === 1 || isNaN(port)) return 80;
    return port;
}
  • It gets the base url from the string.
  • It splits the base url into parts, by ':'.
  • It tries to parse the digits-only part of the port (the last element of the parts array) into an integer.
  • If the URL starts with 'http' AND the port is not a number or the length of the URL parts array is less than 3 (which means no port was implied in the URL string), it returns the default HTTP port.
  • Same thing goes for 'https'.
  • If the length was 1, it means no protocol nor port was provided. In that case or in the case the port is not a number (and again, no protocol was provided), return the default HTTP port.
  • If it passes through all these tests, then it just returns the port it tried to parse into an integer at the beginning of the function.

Here is a regex based solution (the regex is not bullet proof):

var urls = [
  "http://localhost/path/",
  "https://localhost/",
  "http://localhost:8080",
  "https://localhost:8443/path",
  "ftp://localhost/"
];
var i;
for (i = 0; i < urls.length; i++) {
  console.log(urls[i], getPortFromURL(urls[i]));
}

function getPortFromURL(url) {
  var regex = /^(http|https):\/\/[^:\/]+(?::(\d+))?/;
  var match = url.match(regex);
  if (match === null) {
    return null;
  } else {
    return match[2] ? match[2] : {http: "80", https: "443"}[match[1]];
  }
}
<!-- nothing here, see console -->
like image 38
Salman A Avatar answered Dec 06 '25 23:12

Salman A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!