Monday, August 12, 2019

How to do a wildcard / GREP / regex find-replace in Illustrator?


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?



Answer



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
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 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)




No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...