Closed Bug 379341 Opened 19 years ago Closed 19 years ago

Javascript regular expression with global tag scope not honored

Categories

(Firefox :: General, defect)

x86
Windows Vista
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 98409

People

(Reporter: ian.limn, Unassigned)

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 Using a regular expression with a global tag multiple times produces incorrect results. For instance, calling this function twice will return true, false. Expected result is true, true. function globalRegex() { return /foo/g.test("foo"); } I struck this on a intranet page that uses a function to do client-side form validation. As there were multiple fields of a particular data type, there were repeated calls to the validation function. For the 1st field checked the expected result occured, but not for the 2nd. This html demonstrates the bug. <html> <body> <script language="JavaScript" type="text/javascript"> function globalRegex() { return /foo/g.test("foo"); } document.writeln(globalRegex()); document.writeln(globalRegex()); </script> </body> </html> Reproducible: Always Steps to Reproduce: Open page with this html <html> <body> <script language="JavaScript" type="text/javascript"> function globalRegex() { return /foo/g.test("foo"); } document.writeln(globalRegex()); document.writeln(globalRegex()); </script> </body> </html> Actual Results: true false Expected Results: true true Tried this on a Windows XP with Firefox 1.5 with the same result. I'm pretty sure I've interpreted the use of the 'g' flag correctly. It should match all occurances of the pattern (rather than just the first). It should not effect multiple executions. Arguably the 'g' flag is nonsensical when used with the '.test' function, but it may be common to define a regex pattern once and reuse it for '.test' and '.replace'. Which is what happened in the example where I found it.
See also bug 237111.
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.