AS3 : RegExp exec method in loop problem

Posted by Boun on Stack Overflow See other posts from Stack Overflow or by Boun
Published on 2010-03-16T15:50:47Z Indexed on 2010/03/16 15:51 UTC
Read the original article Hit count: 530

Filed under:
|
|

Hi everyone,

I need some help about RegExp in AS3.

I have a simple pattern :

patternYouTube = new RegExp ( "v(?:\/|=)([A-Z0-9_-]+)", "gi" );

This pattern is looking for the youTube id video.

For example :

var tmpUrl : String;    
var result : Object;
var toto : Array = new Array();
toto = [http://www.youtube.com/v/J-vCxmjCm-8&autoplay=1, http://www.youtube.com/v/xFTRnE1WBmU&autoplay=1];
var i : uint;

for ( i = 0 ; i < toto.length ; i++)
{
     tmpUrl = toto[i];
     result = patternYouTube.exec ( tmpUrl );
     if ( result.length != 0 && result != null )
     {
         trace(result);
     }
}

When i == 0, it works perfectly. Flash returns me : v/J-vCxmjCm-8,J-vCxmjCm-8

When i == 1, it fails. Flash returns me : null

When I revert the two strings in my array such as :

toto = [ http://www.youtube.com/v/xFTRnE1WBmU&autoplay=1, http://www.youtube.com/v/J-vCxmjCm-8&autoplay=1 ];

When i == 0, it works perfectly : Flash returns me : xFTRnE1WBmU

When i == 1, it fails : Flash returns me : null

Do you have any idea about the problem in the loop ?

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about regex