6 Replies Latest reply on Apr 29, 2009 10:00 AM by Jed Schneider

    RegEx fails when it should pass test, what gives?

    Jed Schneider Level 1

      I have a question why Flex treats the following two RegEx object differently. in the first I define a regular expression as a string and then pass that to the object. the second time I use a string literal and specify it to ignore case "i", and flex treats it proper-like. What gives?

       

      How do I tell flex to use the "i" in the regular expression string "searchPhrase", or is this not possible.

      ====


      var searchPhrase:String = "so";
      var expression:String = "/" + searchPhrase + "/i";
      trace (
      expression);// "/so/i"

      var regEx:RegExp = new RegEx(expression);

      trace (regEx.soucre);// "/so/i"

      var testString:String = "So";

      trace ( regEx.test(testString) );//false;

      regEx = new RegExp("so", "i");

      trace ( regEx.source ); // "so"
      trace ( regEx.test(testString) );//true;

        • 1. Re: RegEx fails when it should pass test, what gives?
          Barna Biro Level 3

          Hi Jed,

           

          The reason you are getting a different result is because you are not using the regular expression correctly when you are passing the expression as a string. http://livedocs.adobe.com/flex/2/langref/RegExp.html As you'll see, the constructor gets two arguments: RegExp(re:String, flags:String) where the first parameter is the expression and the second one is the flag. When you are doing this:

           

          var searchPhrase:String = "so";
          var expression:String = "/" + searchPhrase + "/i";
          trace (expression);// "/so/i"
          var regEx:RegExp = new RegEx(expression);
          

           

          Then in essence you are doing this: var regEx:RegExp = new RegEx("/so/i"); Which is not what you want because "/so/i", as a whole, is treated as the expression and you are not specifying a flag ( the second parameter is missing from the constructor ). The correct format would be: var regEx:RegExp = new RegEx("so", "i"); or var regEx:RegExp = /so/i;

           

          I hope you understand now,

          With best regards,

          Barna Biro

          • 2. Re: RegEx fails when it should pass test, what gives?
            Jed Schneider Level 1

            It is clear to me that is the case, but why would Adobe set it up this way? It provides me no use to regular expressions written for other platforms. Do I need to use a regular expression to parse the regular expression to use it?

            • 3. Re: RegEx fails when it should pass test, what gives?
              Barna Biro Level 3

              Since I don't work for Adobe and had no contribution to the RegExp class, I can't answer your question. Regular expressions work the same way for any platform, you just need to learn to use the platform correctly. Take a look at this example: http://ryanswanson.com/regexp/#start regular expressions have nothing to do with platforms.

               

              With kind regards,

              Barna Biro

              • 4. Re: RegEx fails when it should pass test, what gives?
                Jed Schneider Level 1

                It just seems Flex should know that the i is a flag after the last forward slash, or accept two kinds of arguments. I saw that tool at ryanswanson's blog, very cool, and useful.

                • 5. Re: RegEx fails when it should pass test, what gives?
                  Barna Biro Level 3

                  Well, RegExp does accept 2 kind of arguments, it is really up to you how you build your expression and how you pass it to the RegExp object. If you want to use a string the way you first tried to build a regular expression then you need to recreate it by removing the elements that are not used when passing the string as the first parameter to the RegExp constructor ( like the forward slash / ) but before you do this, you should first split the string in 2 pieces, you need to get the first part that is the actual expression and the second part that is the flag. You could do this by searching trough the string with the String class' search method and retrieve the index of the last forward slash ( / ). Once you got the index, you can split the string in two, the first part should be until the last slash and the second part from the slash to the end. You should obviously check the second part to see if it's empty or not and what kind of flags were set ( more operations ). Once everything is slipt and cleared of unwanted charaters, you can simply pass the two parts to the RegExp constructor and you should now have a correctly working regular expression.

                   

                  I know that this is not a great solution and is more of a hack but it could be a possible solution.

                   

                  Kind regards,

                  Barna Biro

                  • 6. Re: RegEx fails when it should pass test, what gives?
                    Jed Schneider Level 1

                    Example of why this is dumb:

                     

                    var testString:String = "05/4";

                    var otherTestString:String = "aBc";


                    var alphaRegExp:RegExp = new RegExp("/[a-z0-9\\/]{3}/", "i");//double escape like flex doc says

                    trace (alphaRegExp.source);//[a-z0-9\/]{3}, yup, looks right

                    trace (alphaRegExp.test( testString ) );//false, should be true


                    //ok so I will make a new exp. and the assign it, as the flex doc suggests

                    var alphaRegExp:RegExp = /[a-z0-9\/]{3}/;


                    trace (alphaRegExp.test( testString ) );//true

                    trace (alphaRegExp.test(otherTestString) );//false, bc no "i" flag when I assign it this way == dumb


                    //so

                    alphaRegExp = /[a-zA-Z0-9]\/]{3};

                    trace (alphaRegExp.test( testString ) );//true

                    trace (alphaRegExp.test(otherTestString) );//true