JavaScript String.prototype.lastIndexOf() Method
The lastIndexOf() method returns the position of the last occurrence of some text in a string, or -1.
Description
The lastIndexOf() method returns the index of the last occurrence of the search text, searching backwards, or -1 if it isn't found. A common use is finding the last dot in a file name.
The optional position argument limits the search to matches that start at or before that index, which lets you step backwards through every occurrence in a loop. An empty search string returns the position, or the string's length.
It's useful for working with paths and file names: everything after the last slash is the file name, and everything after the last dot is the extension. For full URLs and paths, the URL object or a library may handle edge cases, such as query strings and dots in folder names, more reliably.
Syntax
string.lastIndexOf(searchString)
string.lastIndexOf(searchString, position)Parameters
- searchStringThe text to search for.
- positionOptional. The index to search backwards from.
Return Value
The index of the last match, or -1 if it isn't found.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.lastIndexOf() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





