JSFiddle - React, Tailwind, and code Playground

JavaScript

indexMatches = [];
    function getMatchIndexes(str, toMatch) {
        var toMatchLength = toMatch.length,
            match,
            i = 0;
    
        while ((match = str.indexOf(toMatch, i)) > -1) {
            indexMatches.push(toMatch);
            i = match + toMatchLength;
        }
        return indexMatches;
    }

    console.log(getMatchIndexes("this is code [table which has [table table [row and rows [table]", "[table"));