var str = "I learn JavaScript, and learning is fun";
alert( str.lastIndexOf("learn") ); // returns 24
var str = "I learn JavaScript, and learning is fun";
alert( str.lastIndexOf("I learn") ); // Found at beginning: returns 0
var str = "I learn JavaScript";
alert( str.lastIndexOf("carrot") ); // Not found: returns -1
alert( "I learn JavaScript".lastIndexOf("ear") ); // returns 3
var str = "I learn JavaScript right now. Learning is fun";
alert( str.lastIndexOf("learn") ); // Returns 2, first instance
alert( str.toLowerCase().lastIndexOf("learn") ); // Returns 30
alert( "I learn JavaScript".lastIndexOf("ear", 5) ); // returns 3
$str = 'I learn PHP right now. Learning is fun';
echo strrpos( $str , 'learn' ); // Returns 2, the first instance
echo strripos( $str , 'learn' ); // Returns 23, the last instance
Sister method: indexOf() also searches for one string in another, but starts at the beginning.