Comment

Eddie

You have a very serious and subtle error in your code. You incorrectly miss some results because RegExp.test doesn't reset the lastIndex of the regex. Thus after a successful match, it will start its next search at that index! You can see the bug in your demo by searching for "django." It doesn't return all the results it should. Here's the MDN documentation on RegExp.test which probably explains the issue better than I do: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Replies

Peter Bengtsson

I'm not surprised it's buggy :)
But what do you mean by searching for "django." in the demo? There's nothing to match for that string in all the titles.

Eddie

Hmm, maybe we are looking at different demos? The demo you linked in this blog post (https://codesandbox.io/s/62x4mmxr0n) should return 20 results when you type in "django" but only returns 18. The word "django" is in two of the first two titles.

Paul

I think the bug is that it's using the "global" flag which is stateful. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
Change flags to "i" and it behaves as expected.