UNPKG

string-indexes

Version:

Retrieves all indexes, in non-overlapping ranges, of a substring in a string.

16 lines (15 loc) 383 B
/* MAIN */ const getIndexes = (str, substr) => { const indexes = []; const substrLength = substr.length; let indexFrom = 0; while (true) { const index = str.indexOf(substr, indexFrom); if (index === -1) return indexes; indexes.push(index); indexFrom = index + substrLength; } }; /* EXPORT */ export default getIndexes;