How to use split() and pop() In Javascript

Using split() and pop(), this approach splits the text at the substring, extracts the last segment, and calculates the index of its start within the original string.

Syntax:

const segments = str.split(substring);
const lastIndex = str.length - segments.pop().length - substring.length;

Example: In this example, the index of the last occurrence of “Science” is found by splitting the string and calculating the index based on the lengths of segments and substring.

JavaScript
function lastIndexOfSubstring(str, substr) {
    let lastIndex = -1;
    for (let i = 0; i <= str.length - substr.length; i++) {
        if (str.substr(i, substr.length) === substr) {
            lastIndex = i;
        }
    }
    return lastIndex;
}


console.log(lastIndexOfSubstring("hello world hello", "hello")); // Output: 12



Output
Last occurrence of Science found at index :26

JavaScript Program to Find the Index of the Last Occurrence of a Substring in a String

Finding the index of the last occurrence of a substring in a string is a common task in JavaScript. The last occurrence of a substring in a string in JavaScript refers to finding the final position where a specified substring appears within a given string. We may want to know the position of the last occurrence of a specific substring within a given string.

There are several methods that can be used to find the index of the last occurrence of a substring in a string in JavaScript, which are listed below:

Table of Content

  • Using lastIndexOf() Method
  • Using Regular Expression
  • Using split() and pop()
  • Using a Loop
  • Using Array.prototype.reduceRight() Method

We will explore all the above methods along with their basic implementation with the help of examples.

Similar Reads

Using lastIndexOf() Method

The lastIndexOf() method in JavaScript is used to find the index of the last occurrence of a specified substring in a string. It searches the string from the end to the beginning and returns the index of the last occurrence of the substring, or -1 if the substring is not found....

Using Regular Expression

Regular expressions provide a powerful way to search for patterns in strings. You can use the RegExp constructor along with the exec() method to find the last occurrence of a substring in a string....

Using split() and pop()

Using split() and pop(), this approach splits the text at the substring, extracts the last segment, and calculates the index of its start within the original string....

Using a Loop

Using a loop, iterate through the string, comparing substrings of the same length as the target substring with it. Track the index of the last occurrence. Return the index found after iterating through the entire string....

Using Array.prototype.reduceRight() Method

The Array.prototype.reduceRight() method applies a function against an accumulator and each value of the array (from right to left) to reduce it to a single value. We can use this method to find the last occurrence of a substring by iterating the string array in reverse....

Contact Us