1 Reply Latest reply on Jan 24, 2010 2:11 AM by dave cragg

    Flex regex bug?


      Not sure if this is a bug but the following regular expression code does not do what I would expect it to do:



      var str1:String = "            // Hello";   
      var str2:String = "  // Hello";
      var str3:String = "// Hello";
      var pattern:RegExp = new RegExp("^\s*//");
      if (str1.match(pattern)) {
           trace("str1 matched");
      if (str2.match(pattern)) {
           trace("str2 matched");
      if (str3.match(pattern)) {
           trace("str3 matched");


      So str1 is a string beginning with two tab characters followed by two single space characters, and str2 is a string beginning with just 2 single space characters. The regex I'm using should basically read in English: match strings beginning with 0 or more whitespace characters followed by two forward slashes.


      Here's the result of that code in the console window:


      str3 matched


      I tried the exact same regular expression in python and perl, and both matched all 3 strings properly. Apparently the carat operator (^) is causing problems for the match. When I remove it from the regular expression, all 3 strings match, but that defeats the purpose of trying to only match strings with starting with two slashes with or without leading whitespace.


      So is this a bug in the Flex/Actionscript regular expression library?



        • 1. Re: Flex regex bug?
          dave cragg Level 2

          The following ways of setting the regualr expression seem to work:


          var pattern:RegExp = new RegExp("^\\s*//");

          var pattern:RegExp = /^\s*\/\//;



          The need for the extra backslash before \s in the first method is mentioned in the docs:


          "In a regular expression that is defined with the RegExp() constructor method, to use a metasequence that begins with the backslash (\) character, such as\d (which matches any digit), type the backslash character twice. "