Check if a String ends with a Substring in JavaScript

avatar

Borislav Hadzhiev

3 min

banner

Photo from Unsplash

Check if a String ends with a Substring in JavaScript #

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.

index.js
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.

The endsWith() method performs a case-sensitive comparison. If you want to ignore the case, convert the string and the substring to lowercase.
index.js
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.

Check if a String ends with a Substring using indexOf() #

To check if a string ends with a substring:

  1. Use the String.indexOf() method to check if the string contains the substring starting at a specific index.
  2. If the method doesn't return -1, the string ends with the substring.
index.js
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:

  1. search value - the substring to search for
  2. from index - from which index onwards to search for the substring in the string

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.

Check if a String ends with a Substring using Regex #

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.

index.js
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.

The forward slashes / / 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.

index.js
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.

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 ยฉ 2023 Borislav Hadzhiev