3

InDesign can do advanced find - change AKA find - replace using GREP rules AKA regex AKA regular expressions which allows all sorts of wildcard rules and automation.

Is there any way to do the same in Illustrator?

9

I gone done made a script for that. I've tested it in Illustrator CS6 on Mac and on Windows.

It does a regex find-replace on text in the selected items, or on all text if nothing is selected.

It works on point text, area text and text on a path, and doesn't mind if non-text items are included in the selection.

Note: there seems to be a bug where sometimes it fails to find text within selected groups - if you encounter this, try running the script with nothing selected, or ungrouping.


Regex text replace.js

var scope = app.activeDocument.selection.length ? app.activeDocument.selection : app.activeDocument.pageItems;

var find = prompt("Find: (Text or GREP/regex)","");
if(find !== null){

    var replace = prompt("Replace: (Text or GREP/regex)","");
    if(replace !== null){

        var changes = 0;

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

            var text = scope[i];

            var string = text.contents;  
            if(typeof string == "string"){
                var newstring = string.replace( new RegExp(find, 'g'), replace);
                if (newstring != string) {
                    changes++;
                    var paragraphsArray = newstring.split("\n");
                    text.paragraphs.removeAll(); 
                    for(var ii=0;ii<paragraphsArray.length;ii++){  
                         text.paragraphs.add(paragraphsArray[ii]);
                    }
                }
            }
        }
        alert( changes==1 ? "1 text object changed" : changes + " text objects changed");
    }
}

Any GREP rule from InDesign should work - I recommend building and testing the rule in InDesign's Paragraph styles > GREP rules with some sample text and Preview ticked, then copying the GREP code over to this script - which is easier than trial-and-error with this script.

In Replace, you can use $1 for anything matched within the first pair of brackets (( to )), $2 for the second, etc.


Here's a few examples I tested with it:

  • Simple find/replace:
    • Find: test
    • Replace: hello
  • Simple find/replace with one wildcard character, matching test, tent, te!t etc. Note - if you want a normal . in your search, you need to put a \ in front of it so it's not treated as a special character, like This be a sentence\. This be another\. - this is true for most punctuation characters:
    • Find: te.t
    • Replace: hello
  • Add a % to all numbers in selection (this matches numbers with and without a decimal points and one comma, like 123, 12.3, 1,234 and 1,234.5 - for continental European style numbers like 1.234,5 switch the , and .):
    • Find: (\d+\,?\d*\.?\d*)
    • Replace: $1%
  • Turn multiple spaces into one space:
    • Find:+ (there's a space before that + which is getting chopped out)
    • Replace: (space)
0
var scope = app.activeDocument.selection.length ? app.activeDocument.selection : app.activeDocument.pageItems;

change to

var scope = app.activeDocument.selection.length ? app.activeDocument.selection : app.activeDocument.textFrames;

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • Will activeDocument.textFrames also target point text, text on a path, etc, or will it only target text frames with a fixed bounding box? – user56reinstatemonica8 Mar 23 '17 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.