Remove http or https from a URL using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Remove 'http://' or 'https://' from a URL in JavaScript

To remove http:// or https:// from a URL, call the replace() method with the following regular expression - /^https?:\/\// and an empty string as parameters.

The replace method will return a new string where the http:// part is removed.

index.js
function removeHttp(url) { return url.replace(/^https?:\/\//, ''); } // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeHttp('https://bobbyhadz.com')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeHttp('http://bobbyhadz.com'));

remove http or https from url

The code for this article is available on GitHub
If you're looking to avoid using regular expressions, scroll down to the next subheading.

We created a reusable function that removes the http:// or https:// part of the given URL.

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.
The forward slashes / / mark the beginning and end of the regular expression.
index.js
function removeHttp(url) { return url.replace(/^https?:\/\//, ''); }
The code for this article is available on GitHub

The caret ^ matches the beginning of the input, in other words, the string has to start the http(s).

The question mark ? matches the preceding item 0 or 1 times. In the example, the question mark makes the s character optional.

Lastly, we have to escape the forward slashes using backslashes.

The second argument we passed to the replace() method is the replacement for the regex match.

We used an empty string because we wanted to remove the http:// part.

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

If you want to avoid using regular expressions, use the String.startsWith method instead.

# Remove 'http://' or 'https://' from a URL using startsWith

This is a two-step process:

  1. Check if the URL starts with https:// or http://.
  2. If it does, use the slice() method to get the part of the URL after the http protocol.
index.js
function removeHttp(url) { if (url.startsWith('https://')) { const https = 'https://'; return url.slice(https.length); } if (url.startsWith('http://')) { const http = 'http://'; return url.slice(http.length); } return url; } // ๐Ÿ‘‡๏ธ "bobbyhadz.com/books" console.log(removeHttp('https://bobbyhadz.com/books')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeHttp('http://bobbyhadz.com'));

remove http or https from url using startswith

The code for this article is available on GitHub

We first check if the URL starts with https://.

If it does, we use the String.slice() method to get the part of the URL after the protocol.

The only argument we passed to the slice method is the start index - the index of the first character to be included in the string.

index.js
// ๐Ÿ‘‡๏ธ "example.com" console.log('http://bobbyhadz.com'.slice(7));

We used the length of the http:// or https:// strings to determine the start index, just so we can avoid hardcoding a magic number.

If the URL doesn't start with http:// or https://, we return the string as is.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev