RexExp sticky flag doesn't work with /^.../ regexps

RESOLVED INVALID

Status

()

Core
JavaScript: Standard Library
RESOLVED INVALID
3 years ago
3 years ago

People

(Reporter: Jakub Czerny, Unassigned)

Tracking

45 Branch
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

3 years ago
Description:
Method exec of sticky RegExp doesn't match from the middle of the string (using lastIndex property).

Steps to reproduce:
1. Open Scratchpad
2. Paste following (code from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky)

'use strict';
(() => {
    var text = 'First line\nsecond line';
    var regex = /^(\S+) line\n?/y;

    var match = regex.exec(text);
    console.log(match[1]);        // logs 'First'
    console.log(regex.lastIndex); // logs '11'

    var match2 = regex.exec(text);
    console.log(match2[1]);       // logs 'Second'
    console.log(regex.lastIndex); // logs '22'

    var match3 = regex.exec(text);
    console.log(match3 === null); // logs 'true'
})();

3. Press 'Run' button

Actual result:
Following error message added to Scratchpad
/*
Exception: TypeError: match2 is null
@Scratchpad/3:11:5
@Scratchpad/3:2:1
*/

Expected result:
see code comments

Note:
It works ok in current 42.0

Version:
Current Nightly (45)
https://hg.mozilla.org/mozilla-central/rev/754b4805a65cab4f3aca99899227acc44ba4fb20
Build ID 20151211030207

Comment 1

3 years ago
See bug 773687, the metacharacter ^ doesn't change its meaning in ES6 even working with the /y flag. The MDC doc should be updated.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 3 years ago
Resolution: --- → INVALID

Comment 2

3 years ago
(In reply to ziyunfei from comment #1)
> See bug 773687, the metacharacter ^ doesn't change its meaning in ES6 even
> working with the /y flag. The MDC doc should be updated.

s/MDC/MDN/. Sorry, typo.

And in your case, you should use the /m flag along with the /y flag.

>var regex = /^(\S+) line\n?/my;
You need to log in before you can comment on or make changes to this bug.