3 Replies Latest reply on Apr 9, 2009 10:24 AM by thebouv

    RegExp alternation with textinput and datagrid

    sanooktuktuk

      I'm wondering if it's possible to filter a datagrid based on more than one word in a textInput box.  Something like 

      return item.text().match(new RegExp(stateName.text, "i")); is easy but can RegExp be used to filter if a user

      has typed in something like "virginia maryland" and have rows in the grid containg either virginia or maryland filtered?

        • 1. Re: RegExp alternation with textinput and datagrid
          thebouv Level 2

          You can build the alternation RegExp dynamically based on what the user enters.  Simple example below:

           

          import mx.controls.Alert;
          
          private function regTest():void {
               
               // string entered by the user
               var myText:String = "maryland virginia";
               // replace their spaces with pipes for alternation
               myText = myText.replace(/\s+/g,"|");
               
               // use the alternation string in a new RegExp
               var myRegEx:RegExp = new RegExp(myText,"g");
               
               // use said RegExp
               var myTest:String = "maryland kentucky";
               myTest = myTest.replace(myRegEx,"foo");
               
               // result is foo kentucky
               Alert.show(myTest);
          }
          
          • 2. Re: RegExp alternation with textinput and datagrid
            sanooktuktuk Level 1

            I kinda adapted your code and came up with this that seems to be working:

             

            private function filterFunc(item:Object):Boolean {

                 var allWords:String=stateName.text;

                 allWords = allWords.replace(/\s+/g,"|");

             

                 return item.text().match(new RegExp(allWords, "i"));

            }

             

               

            • 3. Re: RegExp alternation with textinput and datagrid
              thebouv Level 2

              My example was meant to be verbose and demonstrative, so it looks like it was a help.  Good luck with the rest of your app!