From 650ca70fe660482f7064332d8501e2cd048a9ddc Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Tue, 14 Oct 2014 21:53:01 -0700 Subject: [PATCH] Reset the state of regex in minimongo on matching Fixes #2817. Regular Expressions in JavaScript keep the state when matching for the same string. When there are two documents with the same value of the matching field, the minimongo can incorrectly filter collection because regexp would return different values on different calls. --- packages/minimongo/selector.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/minimongo/selector.js b/packages/minimongo/selector.js index 6113252b32..f676f334c6 100644 --- a/packages/minimongo/selector.js +++ b/packages/minimongo/selector.js @@ -214,6 +214,11 @@ regexpElementMatcher = function (regexp) { // Regexps only work against strings. if (typeof value !== 'string') return false; + + // reset regexp's state to avoid inconsistent matching for objects with the + // same value on consecutive calls of regexp.test + regexp.lastIndex = 0; + return regexp.test(value); }; };