Closed Bug 386049 Opened 17 years ago Closed 17 years ago

Result of the same javascript function not consistent between 2 call (regexp cache bug ?)

Categories

(Toolkit Graveyard :: Error Console, defect)

x86
Windows XP
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 98409

People

(Reporter: coolz_59, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4

Hi !

If I call the same function with local variables 2 times, the results are not the same.

I've read this page : http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:RegExp:exec
"If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string"

So I remove the "g" flag and it's works but as my variables are locals, event if I put "g" flag, it must works, isn't it ?

Sylvain

Reproducible: Always

Steps to Reproduce:
<html>
<head>
<script type="text/javascript">

function test(){
    var xml = '<error>Erreur : vous devez renseigner le nom !</error>' ;
    var re = /<error>(.+?)<\/error>/gm ;
    var tab = re.exec(xml) ;

    if(tab){
        alert('ok');
    }else{
        alert('pas ok');
    }
}
</script>
</head>
<body>
<input type="button" onclick="test()" value="click" />
</body>
</html>

Actual Results:  
1st click : ok
2nd click : pas ok

Expected Results:  
1st click : ok
2nd click : ok

Same results in Firefox 2.0.0.4 (linux), Opera 8.02 (Windows).
IE7 have the excepted results.
Status: UNCONFIRMED → RESOLVED
Closed: 17 years ago
Resolution: --- → DUPLICATE
Hmm.. the bug is "verified invalid" but why if I replace

var re = /<error>(.+?)<\/error>/mg ;
by
var re = new RegExp('<error>(.+?)<\/error>', 'mg') ;

it's works ?

My variables are local so it must always works, isn't it ?

After read the bug 98409, a workaround is to use re.lastIndex=0;
Because in the second case you are creating a new RegExp object each time the code runs. In the first case you are using a reference to the same literal RegExp object which remembers the results of the previous calls to exec.
Ok, thanks for the help.
Product: Firefox → Toolkit
Product: Toolkit → Toolkit Graveyard
You need to log in before you can comment on or make changes to this bug.