Skip to content

Javascript Project to Remove HTTP or HTTPS Protocol From URL

function removeHttp(url) {
  // replace the http and https with an empty string.
  return url.replace(/^https?:\/\//, '');
}

//"example.com"
console.log(removeHttp('https://example.com'));

//"example.com"
console.log(removeHttp('http://example.com'));

function removeHttp(url) {
  // check if the url starts with https.
  if (url.startsWith('https://')) {
    // assign a variable https.
    const https = 'https://';
    // return the url with https
    return url.slice(https.length);
  }
  // check if the url starts with http.

    // assign a variable http.
  if (url.startsWith('http://')) {
    // return the url with http.
    const http = 'http://';
    return url.slice(http.length);
  }
  // return the url.
  return url;
}

//"example.com/images"
console.log(removeHttp('https://example.com/images'));

//"example.com"
console.log(removeHttp('http://example.com'));