New issue
Advanced search Search tips

Issue 600200 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Apr 2016
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: All
Pri: 2
Type: Bug



Sign in to add a comment

RegExp.prototype.test but if searching with /g key for same string twice

Reported by exte...@gmail.com, Apr 3 2016

Issue description

UserAgent: 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
 

Comment 1 by ajha@chromium.org, Apr 5 2016

Cc: ajha@chromium.org
Components: -Blink Platform>DevTools
Labels: -OS-Mac Needs-Feedback OS-All
This seems to be working the same on since older chrome version(30.0.1549.0) to the latest canary(51.0.2700.0) on Windows-7, Mac OS 10.11.3 and Linux Ubuntu 14.04. Same behavior is seen on Firefox as well.

@reporter: Could you please confirm if this works in other browsers or not.

Thank you!


Comment 2 by exte...@gmail.com, 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
Project Member

Comment 3 by sheriffbot@chromium.org, Apr 5 2016

Labels: -Needs-Feedback Needs-Review
Owner: ajha@chromium.org
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

Comment 4 by ajha@chromium.org, Apr 5 2016

Labels: -Needs-Review
Owner: ----
Status: (was: Unconfirmed)
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.
Status: WontFix
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