Copy link to clipboard
Copied
Hello!
Required to make search and replace in a text frame using RegExp.
And at that keep original character formatting of the text frame.
How it's possible?
Bad example – it changes the formating of the entire text to the format of the first character:
var reg = /a/gmi;
var replacer = '*';
var fr = activeDocument.textFrames[0];
fr.contents = fr.contents.replace (reg , replacer);
Thanks!
I didn't tested your code yet.
But why you didn't play with my code snippet and use something like this:
// regex_changeContentsOfWordOrString_RemainFormatting.jsx
// regards pixxxel schubser
var s = /arguments/gi;
var replacer = "other string", result;
var atf = activeDocument.textFrames[0];
while (result = s.exec(atf.contents)) {
try {
aCon = atf.characters[result.index];
aCon.length = result[0].length;
aCon.contents = replacer;
} catch (e) {};
}
Try it and have fun
Copy link to clipboard
Copied
I just go to www.regexpal.com and screw around until I get the result I need for the given purpose and paste the regex back into my code!
And if I do it often enough, it sticks into my human memory!
Copy link to clipboard
Copied
What if I want to insert at the end of the line?