How to get the index of the second occurrence of a word in string in JavaScript

2 Answers

0 votes
const s = "javascript c++ php javascript" 

let i = s.indexOf("javascript", 5); // Find the word beginning at index 5
console.log(i);




/*

run:

19

*/

 



answered Oct 11, 2020 by avibootz
0 votes
const s = "javascript c++ php javascript" 
const tofind = "javascript";

let i = s.indexOf(tofind); 

i = s.indexOf(tofind, tofind.length);

console.log(i);




/*

run:

19

*/

 



answered Oct 11, 2020 by avibootz
...