Docly Developers
Function Updated 17 Oct 2026

String.lastIndexOf(text, [position])

Searches for the last occurrence of a substring within a string.

Parameters

textstringrequired

Sub text to search for within text. The search is case-sensitive.

positionnumberoptional

The latest index where a match may start. Default: the end of the string.

Returns

The index of the last occurrence, or -1 if not found.

Example

Mixed HTML (or any text file) with inline JavaScript between hash signs.
#{
    var text = "Test test";
    var search = "test";
    var notfound = "xyz";
}#

<b>Found in string:</b>
#{
    var index = text.lastIndexOf(search);
    write(index);
}#


<b>Not found in string:</b>
#{
    index = text.lastIndexOf(notfound);
    write(index);    
}#