marklieberman / downloadstar

Download all items in a webpage that match a pattern
GNU General Public License v3.0
90 stars 16 forks source link

Regex Fast Filter is only matching every second item #20

Closed OkanEsen closed 6 years ago

OkanEsen commented 6 years ago

Right now the Regex Fast Filter is only matching every second item in the list, which is caused by setting the global flag for the regular expression, which is causing to search the next candidate by the lastIndex property. So if we have a match the first time, the lastIndex property is updated according to the last match and every subsequent usage of test resets it's index to 0, because there wasn't a match after the last known index.

Example:

let regex = new RegExp(/foo/, 'gi')

// lastIndex: 0
regex.test('https://google.com/foo') // true

// lastIndex: 22
regex.test('https://apple.com/foo') // false

// lastIndex: 0
regex.test('https://amazon.com/foo') // true

...

I'm going to push a Pull request, which fixes this issue by removing the global flag.