How to use lastIndexOf() to get the index within a String of the last occurrence of the specified value in JavaScript

2 Answers

0 votes
// str.lastIndexOf(searchValue[, fromIndex])

var s = 'Online JavaScript Cloud Products';

document.write(s.lastIndexOf('o') + "<br />");
document.write(s.lastIndexOf('o', 25) + "<br />");
document.write(s.lastIndexOf('o', 20) + "<br />");
document.write(s.lastIndexOf('o', 19) + "<br />"); // o
document.write(s.lastIndexOf('O', 19) + "<br />"); // O


/*
run:

26
20
20
-1
0

*/

 



answered Aug 10, 2016 by avibootz
0 votes
// str.lastIndexOf(searchValue[, fromIndex])

var s = 'A Online JavaScript Cloud Online Products';

document.write(s.lastIndexOf('A', 0) + "<br />");
document.write(s.lastIndexOf('A', -3) + "<br />");
document.write(s.lastIndexOf('B') + "<br />");
document.write(s.lastIndexOf('') + "<br />");
document.write(s.lastIndexOf('', 5) + "<br />");
document.write(s.lastIndexOf('Online') + "<br />");
document.write(s.lastIndexOf('ONLINE') + "<br />");


/*
run:

0
0
-1
41
5
26
-1

*/

 



answered Aug 10, 2016 by avibootz
...