Remove Trailing Slashes From A String In JavaScript

If you want to remove trailing slashes from a string in JavaScript, you can use the replace() method or the slice() and endsWith() methods together. Let’s see how it works!

JavaScript remove trailing slashes from a string

Removing Multiple Trailing Slashes From String In JavaScript Using replace()

One way to remove trailing slashes from a string in JavaScript is to use the replace() method. This method accepts a regular expression, which we can use to match any strings that end with a slash. Then, we can use the replace() method to change the trailing slash to an empty string:

let str = ‘https://www.example.com//’;

str = str.replace(/\/+$/, ”);

console.log(str); // ‘https://www.example.com’

As you can see, we first declared a string variable with some URL that contains multiple trailing slashes. Then, we used the replace() method to remove the trailing slashes. The first argument of the replace() method is a regular expression that matches any strings that end with a slash (/). The second argument is an empty string, which will replace the matched strings.

The $ sign in front of the plus sign signifies that we need to match at the end of the string. The plus sign means that we want to allow a single character after the slash, so we can remove all offending slashes in our string.

If you want to remove only one trailing slash, you can use this regular expression: /\/$/. Since we want to remove multiple trailing slashes, we need to match both the last slash and any additional single-character string on the right side: /\/+$.

See the below example –

let str = ‘https://www.example.com/’;

str = str.replace(/\/$/, ”); // Remove trailing slash from a string in JavaScript using the replace() method

console.log(str); // ‘https://www.example.com’​

The above example shows how to use the replace() method with a regular expression to remove single trailing slash from a string in JavaScript.

Note that the replace method doesn’t change the original string, it creates a new string that contains the replaced characters.

Another way to remove trailing slashes from a string in JavaScript is to use both slice() and endsWith().

Removing Trailing Slashes Using slice() And endsWith()

You can also use the slice() method and endsWith() method together to remove trailing slashes from a string. The slice() method is used to extract parts of a string, and the endsWith() method is used to check if a string ends with a specific character or substring.

let str = ‘https://www.example.com/’;

if (str.endsWith(‘/’)) {

str = str.slice(0, -1); // Remove the last character in the string

}else{

//do nothing

}

console.log(str); // ‘https://www.example.com’

First we used the endsWith() method to check if the string ends with a slash (/). If it does, we use the slice() method to extract everything except for the last character in the string.

The slice() method takes two parameters: the first is the starting index, and the second is the ending index (not including). So, if we want to remove the last character in the string, we need to specify the starting index as 0 and the ending index as -1. Using -1 and str.length -1 will both return the last character in the string.

JavaScript index starts at 0, so the last character in a string is at index -1.

If the string doesn’t end with a slash, then we don’t need to do anything.

Note that the slice() method does not change the original string, it only returns a new string.

This method only removes the last slash character in our string, so use it only if that is your requirement.

Conclusion

In this article, we saw how to remove trailing slashes from a string in JavaScript. You can use the replace() method or the slice() and endsWith() methods together to achieve this. Thanks for reading!

Leave a Comment