JSFiddle - React, Tailwind, and code Playground

by kmendes

JavaScript

var text = "Apple PEAR banana pineapple appLE";

function find(text, searchString){
    text = " " + text.toLowerCase();
    searchString = " " + searchString;
    var ocurrences = [];
    var get = function(text, searchString, lastMatchIndex, ocurrences ){
        var found = text.indexOf(searchString,lastMatchIndex);
        if(found > -1){
            ocurrences.push(found);
            lastMatchIndex = found + searchString.length - 1;
            get(text,searchString,lastMatchIndex,ocurrences);
        }
        return ocurrences;
    }
        
   get(text,searchString,0,ocurrences);
    
   return ocurrences.join(",");
}

console.log(find(text,"apple"));