index.js 474 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. const cloneRegexp = require('clone-regexp');
  3. module.exports = (regexp, string) => {
  4. let match;
  5. const matches = [];
  6. const clonedRegexp = cloneRegexp(regexp, {lastIndex: 0});
  7. const isGlobal = clonedRegexp.global;
  8. // eslint-disable-next-line no-cond-assign
  9. while (match = clonedRegexp.exec(string)) {
  10. matches.push({
  11. match: match[0],
  12. subMatches: match.slice(1),
  13. index: match.index
  14. });
  15. if (!isGlobal) {
  16. break;
  17. }
  18. }
  19. return matches;
  20. };