Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

RegExp for multiple search items?

Engaged ,
Aug 30, 2016 Aug 30, 2016

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.contents)) {

            try {

                aCon = allText.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.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!

TOPICS
Scripting
727
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Aug 30, 2016 Aug 30, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 30, 2016 Aug 30, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 30, 2016 Aug 30, 2016

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.contents)) { 

            try { 

                aCon = allText.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.activate(); 

            processDoc(app.activeDocument); 

        }; 

    }; 

changeData();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 30, 2016 Aug 30, 2016

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, withThese);  

     };

};

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 31, 2016 Aug 31, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 31, 2016 Aug 31, 2016

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; 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 31, 2016 Aug 31, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 01, 2016 Sep 01, 2016
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines