Closed Bug 105972 Opened 23 years ago Closed 23 years ago

/^.*?$/ will not match anything

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

VERIFIED FIXED
mozilla0.9.8

People

(Reporter: mozilla, Assigned: rogerl)

Details

(Whiteboard: [In Rhino, this is bug 106548])

Attachments

(2 files)

The result of the following example: /^.*?$/.exec("hello world")[0] is an empty string. If I understand correctly, the *? construct should match the minimum number of characters required to make a complete match, which means it should match the entire string "hello world". On the other hand, /^.*$/.exec("hello world")[0] /^.+?$/.exec("hello world")[0] /^.*?d/.exec("hello world")[0] all return the string "hello world". Perhaps I am simply misinterpreting the spec, in which case I'm sorry for wasting your time. But it seems that it would be more logical that /^.*?$/ should work. I have not had a chance to test this with other JS interpreters.
Reassigning to rogerl. Same behavior in the SpiderMonkey shell: js> /^.*?$/.exec("Hello World") === null true By contrast, Perl 5 matches the whole string. This script: if ("Hello World" =~ /^.*?$/) { print("match: $&\n"); } else { print("NO MATCH!\n"); } produces this output: match: Hello World
Assignee: nboyd → rogerl
OS: Linux → All
Hardware: PC → All
Whiteboard: [SpiderMonkey has the same problem]
Testcase added to JS testsuite: mozilla/js/tests/ecma_3/RegExp/regress-105972.js
Status: NEW → ASSIGNED
Thanks rogerl for the quick turnaround! As part of the reason for filing this bug, though, I want to verify that more complicated cases work with your fix, such as: /^.*?(:|$)/.exec("hello: world") => "hello:" /^.*?(:|$)/.exec("hello: world") => "hello world" (I do not have a source build available to test your patch myself). Thank you!
I'm guessing the second example was supposed to be /^.*(:|$)/.exec("hello: world") (i.e. greedy) That being the case the results are: /^.*?(:|$)/.exec("hello: world") => hello:,: /^.*(:|$)/.exec("hello: world") => hello: world, as expected. I think.
The latter two examples have been added to the testcase above. Both of them already pass in SpiderMonkey and Rhino, without the patch to this bug even being applied. For example, in SpiderMonkey, using the toSource() operator to display the match arrays: ------------- NON-GREEDY CASE (*? operator) ------------- js> /^.*?(:|$)/.exec("Hello: World").toSource() ["Hello:", ":"] ------------- GREEDY CASE (* operator) ------------------ js> /^.*(:|$)/.exec("Hello: World").toSource() ["Hello: World", ""]
> I'm guessing the second example was supposed to be > /^.*(:|$)/.exec("hello: world") Sorry, the example was supposed to be: /^.*?(:|$)/.exec("hello world")
This must be the SpiderMonkey bug if that's what the patch fixes -- or should a twin bug be filed on the JavaScript Engine component of the Browser product? /be
Changing product/component from Rhino/Core to Browser/JS Engine. I have filed bug 106548 as the twin bug against Rhino -
Component: Core → Javascript Engine
Product: Rhino → Browser
Whiteboard: [SpiderMonkey has the same problem] → [In Rhino, this is bug 106548]
cc'ing some more reviewers for this patch -
r=khanson
Comment on attachment 54703 [details] [diff] [review] Allow non-greedy matching up to AND including cpend to match EOL diff -u format please (you should be able to set that as a default cvs diff option, via .cvsrc or a MacCVS option [I hope!]). The only worry I had was that the loop body (not shown due to lack of diff -u context) would load *cp2 when cp2 == cpend, but that seems to be impossible from a quick glance at matchRENodes and the REOP_EOL case. Maybe JS_ASSERT before the if (*cp2 == '\n') test that cp2 < cpend, just to be sure? sr=brendan@mozilla.org with whatever assertion makes sense. I'm recording khanson's r= via the patch manager (kenton, please get in the habit of doing that, it helps mozilla.org gather stats -- thanks). /be
Attachment #54703 - Flags: superreview+
Attachment #54703 - Flags: review+
Brendan's right about loading *cp2 from cpend. If the next node does fail to match, we would merrily attempt to look for a line terminator, eventually from beyond the end of string. New patch (with context) detects that attempt and fails.
Comment on attachment 62050 [details] [diff] [review] Correct overrun error from previous patch I think this is good to go now. Does it need re-review from khanson? /be
Attachment #62050 - Flags: superreview+
Yes, I will re review. =Kenton=
Comment on attachment 62050 [details] [diff] [review] Correct overrun error from previous patch r=khanson
Attachment #62050 - Flags: review+
Wahh, another patch all reviewed and ready to go, but not checked in. Please set target milestone and check in ASAP (by midnight tonight for 0.9.8, or appeal to drivers@mozilla.org during the 0.9.8 freeze -- else 0.9.9 or bust). /be
Nuts, sorry - I had forgotten these were pending. Fix checked in.
Status: ASSIGNED → RESOLVED
Closed: 23 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla0.9.8
Marking Verified FIXED. The above testcase now passes in the debug and optimized JS shell on Linux, WinNT, and Mac9.1.
Status: RESOLVED → VERIFIED
Flags: testcase+
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: