2 Replies Latest reply on Oct 11, 2013 6:19 AM by yatesy321

    Photoshop javascript Regular expression?

    yatesy321

      Hello,

       

      I am looking to determine the name of a file by using a regular expression. I have three files each one has a different file name that I want to change to depending on the input. The three inputs have either _S_, _F_,_B_ in the name and then I want the reg exp to change the name dependant on this. I can get the _F_ and _B_ to work but the file with _S_ in changes to _F_

       

      Javascript is very new to me. This is what i have done.

       

          var Packshot_S = new RegExp("_S_");
          var Planogram_F = new RegExp("_F_");
          var Planogram_B = new RegExp("_B_");
         
          // create another variable that tests the text string against the pattern. If nothing is found, the test will return "-1".
          var TypeTester = FileRefName.search (Packshot_S);
          if (TypeTester == -1) {
              alert("Packshot_S: " + Packshot_S + TypeTester);
              //Is a Packshot
              var View = "S";
              }
         
          var TypeTester = FileRefName.search (Planogram_F);
          if (TypeTester == -1) {
              alert("Planogram_F: " + Planogram_F + TypeTester);
              // Is a Planogram Front
              var View = "B";
              }  
         
          var TypeTester = FileRefName.search (Planogram_B);
          if (TypeTester == -1) {
              alert("Planogram_B: " + Planogram_B + TypeTester);
              // Is a Planogram Back
              var View = "F";
             

      Thanks

       

      Chris

        • 1. Re: Photoshop javascript Regular expression?
          Michael L Hale Level 5

          At first glance it looks to me like you have var View reversed for "B" and "F".

           

          I would combine the RegExp into one expression and convert the multiple if statements into a switch statement.

          var re = new RegExp('_[SFB]_');
          var typeTester = fileRefName.match(re);
          typeTester==null ? "":typeTester = typeTester[0];
          switch( typeTester ){
              case '_S_':
                  alert("Packshot_S: "  + typeTester);
                  var View = "S";
                  break;
              case '_F_':
                  alert("Planogram_F: "  + typeTester);
                  var View = "F";
                  break;
              case '_B_':
                  alert("Planogram_B: " + typeTester);
                   var View = "B";
                   break;
          }
          

          Also the standard JavaScript naming convention is to start variables with a lowercase letter.

          1 person found this helpful
          • 2. Re: Photoshop javascript Regular expression?
            yatesy321 Level 1

            Thank you Michael this has worked for me