JSFiddle - React, Tailwind, and code Playground

by Brunno Romero

JavaScript

(function() {
  String.prototype.allIndexOf = function(string, ignoreCase) {
    if (this === null) {
      return [-1];
    }
    var t = (ignoreCase) ? this.toLowerCase() : this,
      s = (ignoreCase) ? string.toString().toLowerCase() : string.toString(),
      i = this.indexOf(s),
      len = this.length,
      n,
      indx = 0,
      result = [];
    if (len === 0 || i === -1) {
      return [i];
    }
    for (n = 0; n <= len; n++) {
      i = t.indexOf(s, indx);
      if (i !== -1) {
        indx = i + 1;
        result.push(i);
      } else {
        return result;
      }
    }
    return result;
  }
})();

var str = "três tigres tristes para três pratos de trigo, três pratos de trigo para três tigres tristes.";
var idx = str.allIndexOf("três");

console.log(idx);