Closed Bug 1705557 Opened 4 years ago Closed 4 years ago

Inconsistent results between raw regex obj .test versus RegExp.text using the same pattern and test string

Categories

(Core :: JavaScript Engine, defect)

Firefox 84
defect

Tracking

()

RESOLVED INVALID
Tracking Status
firefox87 --- affected
firefox88 --- affected
firefox89 --- affected

People

(Reporter: ted.casey.sf, Unassigned)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15

Steps to reproduce:

const regex1 = RegExp("^[a-zA-Z,\s]+$", 'g');
const str1 = 'table football, foosball';

console.log(/^[a-zA-Z,\s]+$/.test(str1));
console.log(regex1.test(str1));

Actual results:

Same pattern, Same test string results in different results between raw regex versus the RegExp obj

Expected results:

Results should have been the same. Fails in Safari and Chrome as well.

Also,
replace using a RegExp object fails, but a raw regex expression succeeds:
var regex = new RegExp("h");
var somestring = "hello";

console.log(somestring.replace(regex, "");
console.log(somestring.replace(/h/, "");

Pasting the string from comment 1 the first time it gives: true false results and pasting it again it gives an error.
Pasting the string from comment 2 gives the same results after 2 tries: ello ello

Are these the same results you are seeing?
Thanks.

Flags: needinfo?(ted.casey.sf)

It appears the problem is only with RegExp.test (and perhaps RegExp.exec). Yes, the results for the code in the first comment that I'm getting are the same as the one you're getting. I believe the problem is in the RegExp component here and that the raw regex is behaving properly.

On the code in the second comment, apologies. There was an error in my replace code example (missing a paren at the end of the console log statement), which is why you were getting an error. However, when it is fixed, you're right - RegExp.replace and raw regex .replace both work correctly.

So, just to reiterate the bug here is with RegExp.test (I believe).

Flags: needinfo?(ted.casey.sf)

On Chrome I also get an error "Uncaught SyntaxError: Identifier 'regex1' has already been declared".
On Safari I also get "SyntaxError: Can't create duplicate variable: 'str1'".

Managed to reproduce on Windows 10 x64, macOS 10.15 and on Ubuntu 20.04 x64.
I will set a component for this bug and change the status to new.

Severity: -- → S4
Status: UNCONFIRMED → NEW
Component: Untriaged → JavaScript Engine
Ever confirmed: true
Product: Firefox → Core

Awesome, thanks! I'll continue to monitor.

The problem is in this line, where the backslash is not being escaped properly:

const regex1 = RegExp("^[a-zA-Z,\s]+$", 'g');
                                ^^
                               here

If \s is replaced with \\s, then the results are as expected.

Status: NEW → RESOLVED
Closed: 4 years ago
Resolution: --- → INVALID

Indeed, you're correct. Oversight on my part. These damn regex strings get a bit confusing to look at after you've been looking at them for a while.

I know the regex object is different from a regex string passed to RegExp, so an escape of the slash is necessary, but for the sake of consistency, perhaps the object should behave as though it were a string. There are probably a bunch of implications I'm not thinking about right (to say nothing of backwards compatibility), but just thought I would mention it.

For now, I agree with closing this.

(In reply to Iain Ireland [:iain] from comment #7)

The problem is in this line, where the backslash is not being escaped properly:

const regex1 = RegExp("^[a-zA-Z,\s]+$", 'g');
                                                      ^^
                                                     here

If \s is replaced with \\s, then the results are as expected.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: