Return substring instance count - 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function searchSubstr( fullText, searchText, allowOverlap ){
if (arguments.length < 3) {
allowOverlap = true;
}
if (searchText === '') return 0;
var pos = 0,
count = 0;
while (fullText.indexOf(searchText, pos) > -1) {
pos = fullText.indexOf(searchText, pos) + (allowOverlap ? 1 : searchText.length);
count++;
}
return count;
}