Closed Bug 483196 Opened 16 years ago Closed 16 years ago

Javascript regular expression including global flag and "end of input" special char gives unexpected results

Categories

(Firefox :: General, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 98409

People

(Reporter: jonathan.pasquier, Unassigned)

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ca; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; ca; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 Testing for a pattern with a javascript regex that includes the global flag and the "end of input" ($) character leads to unexpected results. Reproducible: Always Steps to Reproduce: 1. Run the following javascript code: for (var i = 0; i < 4; i++) console.info("Single line: " + /^[a-z]+$/g.test("test")); console.info("----") for (var i = 0; i < 9; i++) console.info("Multiline: " + /^[a-z]+$/gm.test("test\ntest")); 2. Look in the result in the firebug console Actual Results: This is what can be seen in the firebug console: Single line: true Single line: false Single line: true Single line: false ---- Multiline: true Multiline: true Multiline: false Multiline: true Multiline: true Multiline: false Multiline: true Multiline: true Multiline: false Expected Results: The test should always return true in this case An easy workaround is to remove the g flag form the regexp
Summary: Javascript regular expression including glabl flag and "end of input" special char gives unexpected results → Javascript regular expression including global flag and "end of input" special char gives unexpected results
(In reply to comment #0) > An easy workaround is to remove the g flag form the regexp That's not a work-around but the correct thing to do.
Status: UNCONFIRMED → RESOLVED
Closed: 16 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.