Borislav Hadzhiev
Reading timeยท3 min
Photo from Unsplash
Use the endsWith()
method to check if a string ends with a substring, e.g.
if (str.endsWith(substring)) {}
.
The endsWith
method returns true
if the string ends with the substring,
otherwise, false
is returned.
const str = 'one two'; const substr = 'two'; if (str.endsWith(substr)) { // ๐๏ธ this runs console.log('โ string ends with substring'); } else { console.log('โ๏ธ string does NOT end with substring'); }
We used the String.endsWith method to check if a string ends with a specific substring.
The method returns true
if the condition is met and false
otherwise.
endsWith()
method performs a case-sensitive comparison. If you want to ignore the case, convert the string and the substring to lowercase.const str = 'one TWO'; const substr = 'two'; if (str.toLowerCase().endsWith(substr.toLowerCase())) { // ๐๏ธ this runs console.log('โ string ends with substring'); } else { console.log('โ๏ธ string does NOT end with substring'); }
Alternatively, you can use the String.indexOf()
method.
To check if a string ends with a substring:
String.indexOf()
method to check if the string contains the
substring starting at a specific index.-1
, the string ends with the substring.function endsWith(str, substr) { return str.indexOf(substr, str.length - substr.length) !== -1; } console.log(endsWith('hello', 'llo')); // ๐๏ธ true console.log(endsWith('hello', 'bye')); // ๐๏ธ false console.log('hello'.length - 'llo'.length); // ๐๏ธ 2
We used the String.indexOf method to check if a string ends with a substring.
The indexOf()
method returns the index of the first occurrence of a substring
in a string.
If the substring is not contained in the string, the method returns -1
.
We passed the following arguments to the method:
We want to check if the string ends with the substring, so we subtract the substring's length from the string's length to get the from index.
To check if a string ends with a substring, use the test()
method with a
regular expression that matches the substring at the end of the string.
The test
method will return true
if the string ends with the substring,
otherwise, false
is returned.
const str = 'one two'; if (/two$/.test(str)) { // ๐๏ธ this runs console.log('โ string ends with substring'); } else { console.log('โ๏ธ string does NOT end with substring'); }
We used the RegExp.test method to check if a string ends with a specific substring.
The method returns true
if the regular expression is matched in the string and
false
otherwise.
/ /
mark the start and end of the regular expression.The dollar sign $
matches the end of the input.
In the example, we check if the string one two
ends with the substring
two
.
If you want to make the regular expression case-insensitive, add the i
flag.
const str = 'one TWO'; if (/two$/i.test(str)) { console.log('โ string ends with substring'); } else { console.log('โ๏ธ string does NOT end with substring'); }
The i
flag allows us to perform a case-insensitive search in the string.
If you ever need help reading a regular expression, check out this regular expression cheatsheet by MDN.
It contains a table with the name and the meaning of each special character with examples.