Last updated: Mar 1, 2024
Reading timeยท5 min
To remove the last N characters from a string, call the slice()
method with
a start index of 0
and an end index of -N
.
For example, str.slice(0, -2)
will return a new string with the last 2
characters of the original string removed.
const str = 'bobbyhadz.com'; // โ Remove the last 2 characters from a string const removeLast2 = str.slice(0, -2); console.log(removeLast2); // ๐๏ธ bobbyhadz.c // โ Remove the last 3 characters from a string const removeLast3 = str.slice(0, -3); console.log(removeLast3); // ๐๏ธ bobbyhadz.
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:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
The String.slice()
method can be passed a negative index to count backward.
const str = 'bobbyhadz.com'; // โ Remove the last 3 characters from a string console.log(str.slice(0, -3)); // ๐๏ธ bobbyhadz.
Using an end index of -3
and str.length - 3
is the same.
We instruct the slice()
method to go up to, but not including the last 3
characters in the string.
const str = 'bobbyhadz.com'; // โ Remove the last 3 characters from a string const removeLast3 = str.slice(0, -3); console.log(removeLast3); // ๐๏ธ bobbyhadz. // ------------------------------------------------- // โ Same as above const removeLast3_ = str.slice(0, str.length - 3); console.log(removeLast3_); // ๐๏ธ bobbyhadz.
0
and the index of the last character is equal to str.length - 1
.Note that the String.slice()
method doesn't change the original string, it
returns a new string. Strings are immutable in JavaScript.
If you try to remove more characters than there are in the string, the slice()
method returns an empty string.
const str = 'bobby'; // โ Remove the last 100 characters from a string const removeLast100 = str.slice(0, -100); console.log(removeLast100); // ๐๏ธ ""
We tried to remove the last 100 characters from a string that only contains 5
characters, so the slice()
method returned an empty string.
If you have to do this often, define a reusable function.
function removeLastNchars(str, n) { return str.slice(0, -n); } const str = 'bobbyhadz.com'; const removeLast2chars = removeLastNchars(str, 2); console.log(removeLast2chars); // ๐๏ธ bobbyhadz.c const removelast3chars = removeLastNchars(str, 3); console.log(removelast3chars); // ๐๏ธ bobbyhadz.
The removeLastNchars
function takes a string and n
as parameters and removes
the last N characters from the string.
Alternatively, you can use the String.substring()
method.
const str = 'bobbyhadz.com'; // โ Remove the last 2 characters from a string const removeLast2 = str.substring(0, str.length - 2); console.log(removeLast2); // ๐๏ธ bobbyhadz.c // โ Remove the last 3 characters from a string const removeLast3 = str.substring(0, str.length - 3); console.log(removeLast3); // ๐๏ธ bobbyhadz.
The String.substring() method returns a slice of the string from the start index to the excluding end index.
The method takes the following parameters:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
Notice that the substring()
method doesn't support negative indexing.
The end index is exclusive (up to, but not including), so specifying an end
index of str.length - 2
means "go up to, but not including the last 2
characters of the string".
We used the slice()
and substring()
methods in a similar way to remove the
last N characters from a string.
However, there are a couple of
differences
between the String.substring()
and the String.slice()
methods:
substring()
method swaps its start and end index if the start index is
greater than the end index. The slice()
method returns an empty string in
this case.const str = 'bobby'; console.log(str.substring(3, 0)); // ๐๏ธ bob console.log(str.slice(3, 0)); // ๐๏ธ ''
substring()
are negative, they are
treated as if they were 0
.const str = 'bobby'; console.log(str.substring(-3)); // ๐๏ธ bobby console.log(str.slice(-3)); // ๐๏ธ bby
When given a negative index, the slice()
method counts backward from the end
of the string to find the indexes.
The slice()
method is implemented more intuitively and should be your
approach.
Use the String.replace()
method to remove the last N characters from a string
conditionally.
The method will only remove the last N characters from the string if the string ends with the specified characters.
const str = 'bobbyhadz.com'; // โ remove the last 2 characters from a string conditionally const removeLast2 = str.replace(/om$/, ''); console.log(removeLast2); // ๐๏ธ bobbyhadz.c // โ remove the last 3 characters from a string conditionally const removeLast3 = str.replace(/com$/, ''); console.log(removeLast3); // ๐๏ธ bobbyhadz.
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A 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.
The forward slashes / /
mark the beginning and end of the regular expression.
const str = 'bobbyhadz.com'; const removeLast2 = str.replace(/om$/, ''); console.log(removeLast2); // ๐๏ธ bobbyhadz.c
We then specified the characters om
and a dollar sign $
.
The dollar sign $
matches the end of the input.
In other words, the om
characters are matched only if they are at the end of
the string.
The second argument we passed to the replace()
method is the replacement
string.
We replace the match with an empty string in order to remove the characters.
If the string doesn't end with the specified characters, the entire string is returned as is.
const str = 'bobbyhadz.com'; const result = str.replace(/bobby$/, ''); console.log(result); // ๐๏ธ bobbyhadz.com
The String.replace()
method returns a new string with the matches of the
pattern replaced. The method doesn't change the original string.
Strings are immutable in JavaScript.
You can learn more about the related topics by checking out the following tutorials: