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

RegExp search and replace, keep original text formatting

Engaged ,
Aug 06, 2016 Aug 06, 2016

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

find_and_replace_regexp.jpg

Thanks!

TOPICS
Scripting

Views

5.8K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Aug 11, 2016 Aug 11, 2016

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

Votes

Translate

Translate
Adobe
Valorous Hero ,
Aug 21, 2016 Aug 21, 2016

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!

Votes

Translate

Translate

Report

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
Explorer ,
Jun 10, 2019 Jun 10, 2019

Copy link to clipboard

Copied

LATEST

What if I want to insert at the end of the line?

Votes

Translate

Translate

Report

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