1 Reply Latest reply on Jan 26, 2010 9:13 AM by marco.rossi

    variable scope local, pattern match compactly




      I read lines from a file and would like do something different with the line depending on whether it contains a single word or two words.


      var rxA:RegExp = /^\s*(\S+)\s+(\S+)\s*$/;

      var rxB:RegExp = /^\s*(\S+)\s*$/;

      str is the line I read from a file

      if( rxA.test(str) ){

          var match:Object = rxA.exec(str);

          do something with match[1] and match[2];


      if( rxB.test(str) ){

          var match:Object = rxB.exec(str);

          do something with match[1];



      With this code I get an error message, the object name "match" is used twice. Is it possible to make this variable local such that no other routine outside the "if" block can see this object?


      Is it possible to do not only the test, but the exec also inside the if( ... ), like this (similar to Perl):


      if( var match:Object = rxA.exec(str) ){

          do something with match[1] and match[2];