findall-string

Find all occurrences of a string in another string.

Usage no npm install needed!

<script type="module">
  import findallString from 'https://cdn.skypack.dev/findall-string';
</script>

README

find all occurrences of a string in another.

use it like this:

var findall = require('findall-string')
findall('house', 'a house is a house is a house') // [2, 13, 24]

this is the full code of this package, for your delight:

module.exports = function findall (needle, haystack) {
  var prev = 0
  var indexes = []

  while (true) {
    var r = haystack.search(needle)
    if (r === -1) {
      return indexes
    }

    indexes.push(r + prev)
    prev = r + prev + 1
    haystack = haystack.slice(r + 1)
  }
}

no regex.