Re: La lumiere ? [Fut : regexp, match]
Le 25/09/2007 10:55, Pierre Goiffon a écrit :
>>
>> Comme tu l'as déjà constaté, ne pas mettre le flag g résoud le problème
>> (dans ce cas, lastIndex est remis à zéro au début de l'appel de exec).
>
> Extrait de la doc Netscape "JavaScript 1.3 Client Reference", sur la
> méthode exec :
Merci de ton intervention, mais la question n'est plus vraiment sur le
fonctionnement de exec(). Elle est plutôt sur l'initialisation des
variables. Tiens, voici l'exemple que tu as cité modifié pour le mettre
dans une fonction.
<SCRIPT LANGUAGE="JavaScript1.2">
function testg() {
myRe=/ab*/g;
str = "abbcdefabhabbbz"
document.writeln("First match starts at " + myRe.lastIndex)
myArray = myRe.exec(str);
document.writeln("Found " + myArray[0] +
". Next match starts at " + myRe.lastIndex)
mySecondArray = myRe.exec(str);
document.writeln("Found " + mySecondArray[0] +
". Next match starts at " + myRe.lastIndex + "<br>")
}
testg();
testg();
</SCRIPT>
Résultat :
First match starts at 0 Found abb. Next match starts at 3 Found ab. Next
match starts at 9
First match starts at 9 Found abbb. Next match starts at 14
Ma question :
Pourquoi lastIndex n'est-il pas mis à 0 lorsque myRe est réinitialisée
au début du deuxième appel de testg() ?
|