8 Replies Latest reply on Sep 1, 2016 1:02 PM by Silly-V

    RegExp for multiple search items?

    subieguy2 Level 1

      So I am wanting to find a part number in a document. There could be several part numbers. Thanks to some of the users here I have code that currently looks like this....

       

      #target illustrator
      
      
      function changeData() {
          function processDoc(doc) {
      
      
              // OLD PART NUMBER
              var oldPart = /111-1111/gi;
              // NEW PART NUMBER
              var replacePart = "111-1111",
                  result;
      
      
              var allText = doc.textFrames;
      
      
              // SEARCH AND CHANGE LOOP FOR PART #
              while (result = oldPart.exec(allText[i].contents)) {
                  try {
                      aCon = allText[i].characters[result.index];
                      aCon.length = result[0].length;
                      aCon.contents = replacePart;
                  } catch (e) {};
              }
      
      
              // SCAN THRU ALL OPEN DOCUMENTS
              for (var i = 0; i < app.documents.length; i++) {
                  app.documents[i].activate();
                  processDoc(app.activeDocument);
              };
          };
          
        changeData();
      

       

      Now please excuse my lack of knowledge but I was wondering if it is possible to do a list search and list change. I know this isn't the syntax to use but just to get the point across....

       

      // OLD PART NUMBER
      var oldPart = /111-1111 || 222-2222 || 333-3333/gi;
      // NEW PART NUMBER
      var replacePart = "444-4444 || 555-555 || 666-6666", result;
      

       

      So if part 111-1111 is found it would change it to 444-4444. if part 222-2222 is found it would change it to 555-5555. if part 333-3333 is found it would change it to 666-6666.

      Can anyone explain how to do this within my function??

       

      Thanks in advance!

        • 1. Re: RegExp for multiple search items?
          Silly-V Adobe Community Professional

          Looks like you can just put your old part numbers and new ones into 2 arrays and go through them.

          • 2. Re: RegExp for multiple search items?
            subieguy2 Level 1

            Ok so how do you apply an array to a regexp?

            • 3. Re: RegExp for multiple search items?
              subieguy2 Level 1

              Ok so I understand how to search for multiple items in a regexp...but how do i make it match to an array position for the new part or results???

               

              #target illustrator  
              function changeData() {  
                  function processDoc(doc) {  
                      // OLD PART NUMBER  
                      var oldPart = /111-1111|222-2222|333-3333/gi;  
                      // NEW PART NUMBER  
                      var replacePart = "111-1111", result;  
                      var allText = doc.textFrames;   
                      // SEARCH AND CHANGE LOOP FOR PART #  
                      while (result = oldPart.exec(allText[i].contents)) {  
                          try {  
                              aCon = allText[i].characters[result.index];  
                              aCon.length = result[0].length;  
                              aCon.contents = replacePart;  
                          } catch (e) {};  
                      }  
                      // SCAN THRU ALL OPEN DOCUMENTS  
                      for (var i = 0; i < app.documents.length; i++) {  
                          app.documents[i].activate();  
                          processDoc(app.activeDocument);  
                      };  
                  };  
              
              
              changeData();
              
              • 4. Re: RegExp for multiple search items?
                Silly-V Adobe Community Professional

                Just use a simple array and make the 'process all documents' loop be inside of a loop that goes through your array of items to replace.

                var replaceThese = ["111-1111", "222-2222"];

                var withThese = ["333-3333", "555-5555"];

                for(var i=0; i<replaceThese.length; i++){

                     for (var j = 0; j < app.documents.length; j++) {  

                       app.documents[j].activate();  

                       processDoc(app.activeDocument, replaceThese[i], withThese[i]);  

                     };

                };

                Of course you will have to modify your processDoc function to accept the replacee with the replacer as 2 additional arguments.

                1 person found this helpful
                • 5. Re: RegExp for multiple search items?
                  subieguy2 Level 1

                  I understand what you are saying and thank you for that. However I guess I didn't give all the information....sorry about that.

                   

                  The part number I am looking to replace is within a sentence (meaning it isn't in a text frame by itself).

                  Plus I also need to keep the formatting.

                   

                  Here is an example of what the document would contain.

                   

                  PART NUMBER: 111-1111, CHANGE: 05, VERSION: -    (All of that is in one text frame)

                  PART NUMBER: 222-2222, CHANGE: 01, VERSION: HE   (All of that is in one text frame)

                   

                  Both of the above text frames could be on one document....or could be on separate documents.

                  • 6. Re: RegExp for multiple search items?
                    Silly-V Adobe Community Professional

                    Ya so you would need to change up your processDoc function to go something like this:

                        function processDoc(doc, oldTxt, newTxt) {   

                            // OLD PART NUMBER   

                            var oldPart = new RegExp(oldTxt, "gi");   

                            // NEW PART NUMBER   

                            var replacePart = newTxt, result; 

                    • 7. Re: RegExp for multiple search items?
                      subieguy2 Level 1

                      I'm sorry Silly-V.... I appreciate your help but this is more advanced than I know how to put together. I'm new to regexp and I don't usually do functions.... lol

                       

                      So basically what I am saying is I am a graphic designer and suck at programming.

                      • 8. Re: RegExp for multiple search items?
                        Silly-V Adobe Community Professional

                        Don't worry you don't suck, you can do it, you have the awesome powers of greatness and you can overcome any obstacle!