Open In App

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

Last Updated : 21 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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

Approach 1: 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.

Syntax:

str.lastIndexOf(searchValue , index)

Example: In this example, we find the last occurrence index of the substring “Computer” in the text. It displays the index if found; otherwise, indicates absence.

Javascript




const text = "GeeksforGeeks, A Computer science Portal.";
  
// Substring to search for
const substring = "Computer";
  
// Using lastIndexOf() method
const lastIndex = text.lastIndexOf(substring);
  
if (lastIndex !== -1) {
    console.log(`Last occurrence of '
    ${substring}
    ' found at index ${lastIndex}`);
} else {
    console.log(
        `'${substring}' not found in the text.`);
}


Output

Last occurrence of '
    Computer
    ' found at index 17

Approach 2: 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.

Syntax:

const regex = new RegExp(substring, 'g');

Example: In this example,we are using a RegExp and exec(), to finds the last occurrence index of the substring “sample” in the text and displays it.

Javascript




const text = "Hello, this is a sample text. This text is a sample.";
  
// Substring to search for
const substring = "sample";
  
// Create a regular expression
const regex = new RegExp(substring, 'g');
let match;
let lastIndex = -1;
  
while ((match = regex.exec(text)) !== null) {
    lastIndex = match.index;
}
  
if (lastIndex !== -1) {
    console.log(`Last occurrence of '
    ${substring}
    ' found at index 
    ${lastIndex}`);
} else {
    console.log(`'${substring}
    ' not found in the text.`);
};


Output

Last occurrence of '
    sample
    ' found at index 
    45

Approach 3: 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.

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




const str = "GeeksforGeeks, A Computer Science Portal.";
  
const substring = "Science";
const segments = str.split(substring);
const lastIndex = str.length - segments.pop().length - substring.length;
console.log("Last occurrence of Science found at index :"+lastIndex);


Output

Last occurrence of Science found at index :26


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads