Check if String ends with Substring in JavaScript

avatar

Borislav Hadzhiev

Thu Oct 21 20213 min read

banner

Photo by Hannah Skelly

Check if String ends with Substring #

To check if a string ends with a substring, call the endsWith() method on the string, passing it the substring as a parameter. The endsWith method returns true if the string ends with the substring, otherwise it returns false.

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 determine if a string ends with a specific substring.

The method returns true if the condition is met and false otherwise.

The endsWith method performs case sensitive search, if you want to ignore the case, convert the string and the substring to lowercase before making the comparison.
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'); }
The endsWith method is not supported in Internet Explorer. If you have to support the browser, use the next approach covered in this article.

Check if String ends with Substring using indexOf #

In this example, we use the String.indexOf method to check if a string ends with a substring.

index.js
// Supported in IE 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

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 parameters 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

Because we want to check if the string ends with the substring, we subtract the substring's length from the string's length to get the from index.

This approach is not as elegant or direct as using the endsWith method, however, if you have to support Internet Explorer, it gets the job done.

Check if String ends with 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 will be 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 ever need help reading a regular expression, bookmark this regex cheatsheet from MDN. It's by far the best one out there.

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 case insensitive search in the string.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee