RegExp.prototype.test but if searching with /g key for same string twice
Reported by
exte...@gmail.com,
Apr 3 2016
|
||||
Issue descriptionUserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 Steps to reproduce the problem: var a = /a/g; var s = 'a'; a.test(s); // true a.test(s); // false Ok, maybe, according to this (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test#Description): > As with exec() (or in combination with it), test() called multiple times on the same global regular expression instance will advance past the previous match. It works correctly, so it skips previous found string and searches for next (am I right understanding it? It isn't really obvious). But what about this: var s1 = 'a1'; var s2 = 'a2'; var s3 = 'a'; a.test(s); // true a.test(s1); // true a.test(s2); // true a.test(s3); // false Wow, really? It is not the same string, isn't it? Imagine I trying to check is any of these objects values is true: { s1: 'a', s2: 'a', s3: 'a1' } I'd expect to have all of the true, but I'll have only two of them What is the expected behavior? What went wrong? The result is unexpected. Did this work before? N/A Chrome version: 49.0.2623.110 Channel: stable OS Version: OS X 10.11.4 Flash Version: Shockwave Flash 21.0 r0
,
Apr 5 2016
Yes, it looks like Firefox/Safari also works same: > var a = /a/g < undefined > var s= 'a' < undefined > a.test(s) < true = $2 < false = $3 < true = $2 < false = $3
,
Apr 5 2016
Thank you for providing more feedback. Adding requester "ajha@chromium.org" for another review and adding "Needs-Review" label for tracking. For more details visit https://sites.google.com/a/chromium.org/dev/issue-tracking/autotriage - Your friendly Sheriffbot
,
Apr 5 2016
,
Apr 6 2016
This is not a bug, the test method can not tell if its argument is the same string or not, since Strings in Javascript are primitives.
,
Apr 6 2016
The following snippet produces for me the following results: var a = /a/g; var s = 'a'; var s1 = 'a1'; var s2 = 'a2'; var s3 = 'a'; console.log(a.test(s)); // true console.log(a.test(s1)); // false console.log(a.test(s2)); // true console.log(a.test(s3)); // false Which just how the global regex works, as it advances past the last match every time. This works as intended. |
||||
►
Sign in to add a comment |
||||
Comment 1 by ajha@chromium.org
, Apr 5 2016Components: -Blink Platform>DevTools
Labels: -OS-Mac Needs-Feedback OS-All