regular expression
Categories
(DevTools :: Console, defect, P2)
Tracking
(Not tracked)
People
(Reporter: telescop603, Unassigned)
Details
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
Steps to reproduce:
Good evening!
I created a regular expression with a sticky flag (should match any non-digit symbol):
let reg = /\D+/y
Then assigned it lastIndex:
reg.lastIndex = 6
And tested with this expression
reg.test('333333D')
Actual results:
It shows false
Expected results:
The result must be true because index 6 is a letter. I've tested this in Chrome and got true.
![]() |
||
Updated•4 years ago
|
Comment 1•4 years ago
|
||
This is only an issue when executing the steps in the devtools console, because the expression evaluation preview executes reg.test('333333D')
, but doesn't reset reg.lastIndex
. If you type reg.test('333333D')
without pressing the enter-key, the preview will show the value true
. When the enter-key is then pressed, the regular expression is executed another time and the result false
is returned. When all steps are enclosed in a block-statement, the correct result true
is returned:
{ let reg = /\D+/y; reg.lastIndex = 6; reg.test('333333D') }
Comment 2•4 years ago
|
||
Nicolas: probably should flag this expression as having side effects? Can you point to where this is handled.
Comment 3•4 years ago
|
||
This is a known issue (see Bug 1609429)
I'm going to mark this bug as a duplicate, and we should probably disabled instant evaluation for regex as we already have bug reports about it.
Reporter | ||
Comment 4•4 years ago
|
||
(In reply to André Bargull [:anba] from comment #1)
This is only an issue when executing the steps in the devtools console, because the expression evaluation preview executes
reg.test('333333D')
, but doesn't resetreg.lastIndex
. If you typereg.test('333333D')
without pressing the enter-key, the preview will show the valuetrue
. When the enter-key is then pressed, the regular expression is executed another time and the resultfalse
is returned. When all steps are enclosed in a block-statement, the correct resulttrue
is returned:{ let reg = /\D+/y; reg.lastIndex = 6; reg.test('333333D') }
Huge thanks for the great explanation! Now I understand why it works like this.
Description
•