diff --git a/index.js b/index.js index b3dc939..7a3de75 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,21 @@ 'use strict'; - module.exports = function (regexp, string) { - var match, matches = []; + if (!string) return []; + + var match = regexp.exec(string); + if (!match) return []; if (!regexp.global) { - match = regexp.exec(string); - return match ? [match] : []; + return [match]; } + var matches = [match]; while (match = regexp.exec(string)) { - matches.push(match); - if (match[0] == '') { - break; + if (match[0] === '') { + regexp.lastIndex++; + } else { + matches.push(match); } } diff --git a/test/test.js b/test/test.js index 7d06cc2..4f84a54 100644 --- a/test/test.js +++ b/test/test.js @@ -44,17 +44,11 @@ describe('for a global regexp', function () { execAll(/.*/g, input).should.eql([assign(['12345'], { index: 0, input: input - }), assign([''], { - index: 5, - input: input })]); execAll(/1?/g, input).should.eql([assign(['1'], { index: 0, input: input - }), assign([''], { - index: 1, - input: input })]); }); });