js return indicie of string of ceritn sub string

Solutions on MaxInterview for js return indicie of string of ceritn sub string by the best coders in the world

showing results for - "js return indicie of string of ceritn sub string"
Allison
11 May 2016
1function getIndicesOf(searchStr, str, caseSensitive) {
2    var searchStrLen = searchStr.length;
3    if (searchStrLen == 0) {
4        return [];
5    }
6    var startIndex = 0, index, indices = [];
7    if (!caseSensitive) {
8        str = str.toLowerCase();
9        searchStr = searchStr.toLowerCase();
10    }
11    while ((index = str.indexOf(searchStr, startIndex)) > -1) {
12        indices.push(index);
13        startIndex = index + searchStrLen;
14    }
15    return indices;
16}