find the last index of a substring in a string in javascript

You can use the lastIndexOf method in javascript to find the last occurrence of a substring within a string. Here's an example code snippet:

index.tsx
const str = "hello world, hello universe";
const substr = "hello";
const lastIdx = str.lastIndexOf(substr);
console.log(lastIdx); // outputs 13
144 chars
5 lines

In this example, lastIndexOf returns the last index position of the substring "hello" which is found at position 13 in the string "hello world, hello universe".

gistlibby LogSnag