How to Remove a Trailing Slash from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Table of Contents

  1. Remove a Trailing Slash from a String in JavaScript
  2. Remove a trailing slash from a String using String.endsWith()

# Remove a Trailing Slash from a String in JavaScript

Use the String.replace() method to remove a trailing slash from a string, e.g. str.replace(/\/+$/, '').

The replace() method will remove the trailing slash from the string by replacing it with an empty string.

index.js
function removeTrailingSlash(str) { return str.replace(/\/+$/, ''); } // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com/')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com///')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com'));

remove trailing slash from string

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

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 first argument we passed to the String.replace() method is a regular expression.

index.js
function removeTrailingSlash(str) { return str.replace(/\/+$/, ''); }

The forward slashes / / mark the beginning and end of the regular expression.

We had to escape the forward slash with a backslash because the forward slash is a special character in regular expressions.

The plus + matches the preceding item (the forward slash) 1 or more times.

The dollar $ sign matches the end of the input.

In its entirety the regular expression matches one or more trailing slashes at the end of the string.

If your string doesn't end with a forward slash, the replace() method returns the string as is, without removing anything.

index.js
function removeTrailingSlash(str) { return str.replace(/\/+$/, ''); } // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com/')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com///')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com'));

The third string - bobbyhadz.com doesn't end with a trailing slash, so the regular expression is not matched in the string and the replace method returns the original string.

The String.replace() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

index.js
const str = 'bobbyhadz.com/'; const result = str.replace(/\/+$/, ''); console.log(result); // ๐Ÿ‘‰๏ธ "bobbyhadz.com" console.log(str); // ๐Ÿ‘‰๏ธ "bobbyhadz.com/"

Strings are immutable in JavaScript.

An alternative approach is to use the String.endsWith method.

# Remove a trailing slash from a String using String.endsWith()

This is a three-step process:

  1. Use the endsWith() method to check if the string ends with a slash.
  2. If it does, use the slice() method to remove the last character from the string.
  3. If it doesn't return the string as is.
index.js
function removeTrailingSlash(str) { return str.endsWith('/') ? str.slice(0, -1) : str; } // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com/')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com'));

remove trailing slash from string using string endswith

The code for this article is available on GitHub

The String.endsWith method returns true if the string ends with the specified substring and false otherwise.

index.js
console.log('bobbyhadz.com/'.endsWith('/')); // ๐Ÿ‘‰๏ธ true console.log('bobbyhadz.com'.endsWith('/')); // ๐Ÿ‘‰๏ธ false

If the string ends with a slash, we use the String.slice() method and return the entire string up to the last character.

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

The String.slice() method can be passed negative indexes to count backward.

index.js
const str = 'bobbyhadz.com'; console.log(str.slice(0, -1)); // ๐Ÿ‘‰๏ธ bobbyhadz.co console.log(str.slice(0, -2)); // ๐Ÿ‘‰๏ธ bobbyhadz.c
JavaScript indexes are zero-based, so the index of the first character in the string is 0 and the index of the last character is str.length - 1.

Using an end index of -1 and an end index of string.length - 1 is the same.

We instruct the String.slice() method to go up to, but not including the last character in the string.

If the string doesn't end with a slash, we simply return the entire string.

index.js
function removeTrailingSlash(str) { return str.endsWith('/') ? str.slice(0, -1) : str; } // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com/')); // ๐Ÿ‘‡๏ธ "bobbyhadz.com" console.log(removeTrailingSlash('bobbyhadz.com'));
The code for this article is available on GitHub

The ternary operator is very similar to an if/else statement.

If the expression to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise, the value to the right of the colon is returned.

Note that this approach doesn't handle the scenario where the string ends with multiple slashes. We only remove the last slash when using the slice method.

If you need to remove one or more trailing slashes from a string, use the String.replace() method from the previous subheading.

# 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