3 Replies Latest reply on Aug 1, 2016 11:35 AM by Chuck Uebele

    If document name contain THIS, but NOT THIS

    squirpy Level 1

      I am in the process of writing the following script:

       

      If the document name contains "front" and "square", do A.

      If the document name contains "front" and NOT "square", do B.

      If the document name contains "back" and "square, do C.

      If the document name contains "back" and NOT "square", do D.

       

      Here's the relevant code with the conditionals in the same order:

       

      for (i = 0; i < documents.length; i++){
          var curDoc = app.activeDocument = app.documents[i];
          var workingName = curDoc.name;
          if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) != null) {...} 
          if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) == null) {...} 
          if(workingName.match(/^*(back)*$/) != null && workingName.match(/^*(square)*$/) != null) {...} 
          if(workingName.match(/^*(back)*$/) != null && workingName.match(/^*(square)*$/) == null) {...} 
      }
      

       

      However, these ARE not returning true and false as expected. As far as I can tell, taken individually, each regex pattern match works correctly.  I suspect the problem lies with the &&, ==null, and !=null parts, but am at a bit of a loss.

       

      Specifically a file named "front.TIF" is being evaluated as TRUE for this conditional:

       

      if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) != null) 
      

      if(workingName.match(/^*(front)*$/) != null && workingName.match(/^*(square)*$/) != null) {...}

      This does not make sense, it does NOT contain the word "square" so the second half of this conditional, and therefore the entire thing, should be false. Instead it's treating the && (and) like an || (or).

       

      Is (regexMatch != null && otherRegexMatch != null) a flawed approach?

        • 1. Re: If document name contain THIS, but NOT THIS
          Chuck Uebele Adobe Community Professional & MVP

          I tried it with this, and the tiff was okay:

           

          #target photoshop
          for (i = 0; i < documents.length; i++){  
              var curDoc = app.activeDocument = app.documents[i];  
              var workingName = curDoc.name;  
          
              if(workingName.match(/(front)/g) && workingName.match(/(square)/g)) {alert(workingName +'   ' +1 +'   '+ workingName.match(/(front)/g) + '   '+workingName.match(/(square)/g) )}   
              if(workingName.match(/(front)/g) && !workingName.match(/(square)/g)) {alert(workingName +'   ' +2 +'   '+ workingName.match(/(front)/g) + '   '+workingName.match(/(square)/g) )}   
              if(workingName.match(/(back)/g) && workingName.match(/(square)/g)) {alert(workingName +'   ' +3)}   
              if(workingName.match(/(back)/g) && !workingName.match(/(square)/g)) {alert(workingName +'   ' +4)}  
              }
          
          • 2. Re: If document name contain THIS, but NOT THIS
            Kukurykus Adobe Community Professional

            That first part of code is uneeded and global mode in RegExp too:

             

            for(var i = 0; i < documents.length; i++){  
                var workingName = documents[i].name
                if (workingName.match(/front/) && workingName.match(/square/)) alert('FS')
                if (workingName.match(/front/) && !workingName.match(/square/)) alert('F')
                if (workingName.match(/back/) && workingName.match(/square/)) alert('BS')
                if (workingName.match(/back/) && !workingName.match(/square/)) alert('B')
            }
            

             

            Btw. this is good place to test your RegEx before implementing in JavaScript code: RegExr: Learn, Build, & Test RegEx

            • 3. Re: If document name contain THIS, but NOT THIS
              Chuck Uebele Adobe Community Professional & MVP

              Yea,I thought about the global part and that it was unneeded. Still trying to wrap my head around regex.