In JS, to implement the last occurrence of a character or string in a lookup string we can use the LastIndexOf () method
String.LastIndexOf (string, num)
String.LastIndexOf (String)
The LastIndexOf () method returns the last occurrence of a specified string value, which is searched forward at the specified position in a string
<script language= "JavaScript" >
<!--
var myString = new String ("Hello World, here I am!");
document.write (Mystring.lastindexof ("e") + ' <br> ');
document.write (Mystring.lastindexof ("L", 3));
Document.close ();
-->
</script>
Tips and comments
Note: the LastIndexOf () method is case sensitive!
Note: If the string value to retrieve does not appear, the method returns-1.
Instance
In this case, we'll do a different search within the "Hello world!" string:
<script type= "text/web Effects"
var str= "Hello world!"
document.write (Str.lastindexof ("Hello") + "<br/>")
document.write (Str.lastindexof ("World") + <br/ > ")
document.write (Str.lastindexof (" World "))
</script> output of above code:
0
-1
6