javascript find all occurrences in string

Solutions on MaxInterview for javascript find all occurrences in string by the best coders in the world

showing results for - "javascript find all occurrences in string"
Anthony
08 May 2016
1var str = "I learned to play the Ukulele in Lebanon."
2var regex = /le/gi, result, indices = [];
3while ( (result = regex.exec(str)) ) {
4    indices.push(result.index);
5}
6console.log(indices) // => [2, 25, 27, 33]
7//find all occurence of le and return the return an array of the indeces