How to use String lastIndexOf() to search from the end of a string in Javascript
Search from the end
lastIndexOf()
searches a string and return the position.
It returns -1 if the substring isn't found.
lastIndexOf()
method does the search from the end of the string.
var stringValue = "hello world";
console.log(stringValue.lastIndexOf("o")); //7
The code above generates the following result.
Start from middle
An optional second argument tells where to start with.
var stringValue = "hello world";
console.log(stringValue.lastIndexOf("o", 6)); //4
The code above generates the following result.