function getAllRegexMatches(regex: RegExp, string: string) { if (regex.constructor !== RegExp) { throw new Error('not RegExp'); } const matches = []; let match = regex.exec(string); if (regex.global) { while (match) { matches.push(match); match = regex.exec(string); } } else if (match) { matches.push(match); } return matches; }