JSFiddle - React, Tailwind, and code Playground

by michapixel

JavaScript

const regex = RegExp('foo*', 'g');
const str = 'american football, foosball, football';

const indexPairs = [];

while (null !== (matchArr = regex.exec(str))) {
    indexPairs.push([matchArr.index, regex.lastIndex]);
}

console.log(indexPairs); // output: [9, 12], [19, 22], [29, 32]