Closed Bug 252356 Opened 20 years ago Closed 20 years ago

RexExp.exec does not advance lastIndex after matching (start of line) ^ or $ (end of line) meta characters in global pattern

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: jeff.grimes, Unassigned)

References

Details

Attachments

(2 files)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040616
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040616

When calling Regex.exec on a pattern that matches the ^ or $ metachars,
lastIndex is not advanced past the match position.

Reproducible: Always
Steps to Reproduce:
1. Create the following test page:

<html>
<head>
<script>
function doRegExp() {
    var regexp = document.getElementById("regexp").value;
    var text = document.getElementById("testInput").value;
    var regexpResult = document.getElementById("regexpResult");
    regexpResult.value = "";
    var pattern = new RegExp(regexp, "g");
    var result;

    var numLoops = 0;
    var maxLoops = 10; /* avoid an infinite loop */
    while ((result = pattern.exec(text)) != null && numLoops < maxLoops) {
        regexpResult.value += "Matched '" + result[0] + "' at positon " +
result.index + "; next search begins at " + pattern.lastIndex + "\n";
        for (var i=1; i < result.length; i++) {
          regexpResult.value += "\tResult " + i + ": " + result[i] + "\n";
        }

        numLoops++;
    }    
}
</script>

</head>
<body>
<div>
Regex: <input type="text" id="regexp" size=70></input><br>
String: <input type="text" id="testInput" size=70></input><br>
<button onclick="doRegExp();">Match</button>
<br>
<textarea id="regexpResult" rows=10 cols=70></textarea>
</div>
</BODY>
</HTML>

2. Open the test page in Mozilla.
3. Type "^|A" (without quotes) in the RegEx: text box
4. Type "AAAAApple" (without quotes) in the String text box
5. Click "Match" and observer ther results

6. Open the test page in Mozilla.
7. Type "$|A" (without quotes) in the RegEx: text box
8. Type "AAAAApple" (without quotes) in the String text box
9. Click "Match" and observer ther results


Actual Results:  
Results of steps 3-5:

Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0

Results of Steps 6-9:

Matched 'A' at positon 0; next search begins at 1
Matched 'A' at positon 1; next search begins at 2
Matched 'A' at positon 2; next search begins at 3
Matched 'A' at positon 3; next search begins at 4
Matched 'A' at positon 4; next search begins at 5
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0


Expected Results:  
Results of steps 3-5:

Matched '' at positon 0; next search begins at 1
Matched 'A' at positon 1; next search begins at 2
Matched 'A' at positon 2; next search begins at 3
Matched 'A' at positon 3; next search begins at 4
Matched 'A' at positon 4; next search begins at 5

Results of steps 6-9 (confirmed in IE):

Matched 'A' at positon 0; next search begins at 1
Matched 'A' at positon 1; next search begins at 2
Matched 'A' at positon 2; next search begins at 3
Matched 'A' at positon 3; next search begins at 4
Matched 'A' at positon 4; next search begins at 5
Matched '' at positon 9; next search begins at 10
I get slightly different results (why does does comment 0 show 15 lines of
results for "Results of Steps 6-9"?) from current trunk JS shell and Firefox 0.8:

Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
 
Matched 'A' at positon 0; next search begins at 1
Matched 'A' at positon 1; next search begins at 2
Matched 'A' at positon 2; next search begins at 3
Matched 'A' at positon 3; next search begins at 4
Matched 'A' at positon 4; next search begins at 5
Matched '' at positon 10; next search begins at 10
Matched '' at positon 10; next search begins at 10
Matched '' at positon 10; next search begins at 10
Matched '' at positon 10; next search begins at 10
Matched '' at positon 10; next search begins at 10

These results conform to ECMA-262 Edition 3.  See 15.10.2.6, which defines
assertions such as ^ and $, and 15.10.6.2 which defines RegExp.prototype.exec.
Notice that nothing in 15.10.2.6 updates the state variable x's members.

If IE behaves differently, that's because IE does not conform to ECMA-262 Ed. 3
in this regard.  Or were you comparing to some other browser's JS engine?

/be
Status: UNCONFIRMED → RESOLVED
Closed: 20 years ago
Resolution: --- → INVALID
Oops, I see my js shell testcase run used AAAAAapple, not AAAAApple.  Fixing
that in this attachment.  Still, the bug is invalid and the results are as
expected.  Here are the results with AAAAApple:

Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
Matched '' at positon 0; next search begins at 0
 
Matched 'A' at positon 0; next search begins at 1
Matched 'A' at positon 1; next search begins at 2
Matched 'A' at positon 2; next search begins at 3
Matched 'A' at positon 3; next search begins at 4
Matched 'A' at positon 4; next search begins at 5
Matched '' at positon 9; next search begins at 9
Matched '' at positon 9; next search begins at 9
Matched '' at positon 9; next search begins at 9
Matched '' at positon 9; next search begins at 9
Matched '' at positon 9; next search begins at 9

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

Attachment

General

Created:
Updated:
Size: