Function Updated 17 Oct 2026
String.lastIndexOf(text, [position])
Searches for the last occurrence of a substring within a string.
Parameters
textstringrequiredSub text to search for within text. The search is case-sensitive.
positionnumberoptionalThe 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);
}#The template above produces this output:
<b>Found in string:</b>
5
<b>Not found in string:</b>
-1