4 Replies Latest reply on Feb 12, 2010 7:42 AM by nikos101

    Help with a regex finction:

    nikos101 Level 2

      I'm trying to do this but not matter what clientNameFilter.text contains I always get false returned

       

       

       

           var pattern:RegExp = new RegExp('/.*' + clientNameFilter.text + '.*/');
                        

           var test :int =     (item.clientName as String).search(pattern) ;
                          if (
                          (item.clientName as String).search(pattern) == -1
                          )
                          {
                              return false;
                          }

       

      Any ideas?

        • 1. Re: Help with a regex finction:
          Gregory Lafrance Level 6

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

           

          Don't need   /  in regex in this case:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
              <![CDATA[
                import mx.controls.Alert;
                private function checkEntry():void {
                  var pattern:RegExp;
                  if(clientNameFilter.text){
                    pattern = new RegExp('.*' + clientNameFilter.text + '.*');
                  }
                  if (txt.text.search(pattern)!=-1){
                    mx.controls.Alert.show("true");
                  }else{
                    mx.controls.Alert.show("false");
                  }
                }
              ]]>
            </mx:Script>
            <mx:HBox>
              <mx:Label text="Your input:"/>
              <mx:TextInput id="txt"/>
            </mx:HBox>
            <mx:HBox>
              <mx:Label text="Filter string:"/>
              <mx:TextInput id="clientNameFilter"/>
            </mx:HBox>
            <mx:Button label="Check" click="checkEntry()"/>
          </mx:Application>
          
          
          1 person found this helpful
          • 2. Re: Help with a regex finction:
            nikos101 Level 2

            Thanks but I'm still having the same problem

             

             

               var pattern:RegExp = new RegExp('.*' + clientNameFilter.text + '.*');
            
                                var test :int =     (item.clientName as String).search(pattern) ;
            
                                if (
            
                                (item.clientName as String).search(pattern) == -1
            
                                )
            
                                {
            
                                    return false;
            
            
            
            
            
            

                                }

            • 3. Re: Help with a regex finction:
              Gregory Lafrance Level 6

              This works for me. If this does not solve your problem, it could be that the data does not get properly respresented as a String, so it always fails. Trace it out and see.

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                <mx:Script>
                  <![CDATA[
                    import mx.controls.Alert;
                    private var pattern:RegExp;
                    private function checkEntry():void {
                      if(clientNameFilter.text){
                        pattern = new RegExp('.*' + clientNameFilter.text + '.*');
                      }
                      if(txt.text.search(pattern) == -1){
                        mx.controls.Alert.show("missing");
                      }else{
                        mx.controls.Alert.show("found");
                      }
                    }
                  ]]>
                </mx:Script>
                <mx:HBox>
                  <mx:Label text="Your input:"/>
                  <mx:TextInput id="txt"/>
                </mx:HBox>
                <mx:HBox>
                  <mx:Label text="Filter string:"/>
                  <mx:TextInput id="clientNameFilter"/>
                </mx:HBox>
                <mx:Button label="Check" click="checkEntry()"/>
              </mx:Application>
              
              


              If this post answers your question or helps, please mark it as such.


              Greg Lafrance - Flex 2 and 3 ACE certified

              www.ChikaraDev.com

              Flex / AIR Development, Training, and Support Services

              • 4. Re: Help with a regex finction:
                nikos101 Level 2

                it works thanks

                 

                 

                trace(item.clientName);
                                    var pattern:RegExp = new RegExp('.*' + clientNameFilter.text + '.*','i');
                                    trace(pattern.source);
                                    var test :int =     (item.clientName as String).search(pattern) ;
                                    if (
                                    (item.clientName as String).search(pattern) == -1
                                    )
                                    {
                                        return false;
                                    }