How to Remove https from URL in JavaScript

JavaScript is a popular framework for web applications. You can easily manipulate elements of a web page using it. In this tutorial you will learn how to Remove https from URL in JavaScript.

Remove https from URL in JavaScript

Method 1 : Using the replace() function

You can remove https from a URL using the replace() function. Inside this function you will use the regular expression and blank space as an argument. As you have to remove the https from a URL you will use the following regular expression.

/^https?:\/\//,

Below is the function to remove https from a URL in JavaScript.

function removeHttps(url) {
  return url.replace(/^https?:\/\//, '');
}
console.log(removeHttp('https://codethebest.com'));

Here I have created a reusable function removeHttps() that will remove https from the URL.

The replace() function returns a new string after replacing all the string matched with the pattern with the space. In this case it is https.

Lets explore more about the pattern.

The ^ is use to match the beginning of the input string that start with the https.

The ? is matches the preceding item 0 or 1 times.

And at last we passed the blank space that is used to replace the pattern with it.

Method 2 : Using startsWith() and slice() function

You can also use the String.startsWith() function to replace the https from the URL.

Use the below lines of code to remove the https from the URL.

function removeHttps(url) {
  if (url.startsWith('https://')) {
    const https = 'https://';
    return url.slice(https.length);
  }
console.log(removeHttp('https://codethebest.com/'));

The startsWith() function will check whether the URL you give contains https or not. If it is then the slice() function will remove the “https://” part from the URL. Here the https:// parts contains 8 characters so I will use slice(7) for that.

Conclusion

In this post you have learned how to remove the HTTPS from the URL. You have learned two methods for that. One is the use of replace() function and the other is startsWith() and slice() function. You can also remove the http from the URL in the same way you have to just define the pattern for the http.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link