GREP search and replace does not maintain original styles of replaced strings

Explorer ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Quick GREP question. If I do want to change this text:

 

Tendrá un nombre:<sup>3</sup> se llamará Manolo.

To this:

Tendrá un nombre<sup>3</sup>se llamará 

 

As you see, all I want is to place the two points (or a comma, o a dot) AFTER the super script. I can easily do this with this search/replace in GREP:

 

Search: ([,.:])(\d)

Replace: $2$1

 

But the end result is this:

 

Tendrá un nombre3<sup>:</sup> se llamará Manolo

 

So, InDesign applies the superscript style to the replaced text.

TOPICS
How to, Type

Views

232

Likes

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

Advisor , May 31, 2021 May 31, 2021
I like Simplicity: just 1 click! (^/) /* _FRIdNGE-0713_InvertText.jsx by FRIdNGE, Michel Allio (^/) The Jedi [31/05/2021] */ // .1 ==> 1. app.findGrepPreferences = null; app.findGrepPreferences.findWhat = "[.,;:]\\d+"; myFound = app.activeDocument.findGrep(); for ( var f = 0 ; f < myFound.length ; f++ ) myFound[f].characters[0].move(LocationOptions.after, myFound[f]); app.findGrepPreferences = null; /* // 1. ==> .1 app.findGrepPreferences = null; app.findGrepPreferences.findWhat = "\\d...

Likes

Translate

Translate
Advisor ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

… Do you talk about "notes"?

 

(^/)  The Jedi

Likes

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 ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Sorry, the <sup></sup> are intended to mean superscript, but I don't know how to write superscript using the text editor in Adobe Help forums.

 

Here an screenshot:

Captura de pantalla 2021-05-31 a las 18.53.32.png

Also, I want to search/replace any of these punctuation characteres: . : ; (hence the search for [.;:])

 

Anyway, thanks for the help.

Likes

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
Adobe Community Professional ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Hi @Nousmedis:

 

I think FRIdNGE is asking if those are plain numeric characters or if they are footnote (or endnote) reference numbers. I'm guessing they are plain numeric characters because I can recreate the problem with your GREP Find/Change and it wouldn't work if they were footnote/endnote numbers. (Unless you just typed up a quick example to use here—let us know!)


Anyway, assuming they are characters, you could use your GREP string as is, but change the formatting to Character Style to [None], and define a GREP style to superscript the numbers. 

 

~Barb 

 

Define a GREP style to superscript the numbers that preceed a colon, period or comma:

g1.png

 

Set up your original Find/Change query and change the Character Format to [None].

g2.png

 

Change all and the GREP style kicks in:

g3.png

 

Likes

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
Adobe Community Professional ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Hi Nousmedis,

I take from your post that this superscript number is not a footnote that is done with InDesign's footnote feature or an endnote that is done with InDesign's endnote feature. And it could help to know if there is a character style or a GREP style applied to the superscript number, to the character before and after.

 

Let's play it safe; my guess is that you need two or perhaps three GREP Find/Change actions to solve the problem.

 

[1] First GREP Find/Change where I also included the white space character, a blank, after the superscript number:

Find:

([,.:])(\d)(\x{0020})

Change to the first found group, the second one, the first again and then the third group:

$1$2$1$3

Plus change the formatting of the found text to unique formatting, a text property that is nowhere else used in your document; could be a special condition from InDesign's Conditional Text feature. A screenshot from my German InDesign illustrates this idea where the Conditional Text panel is named "Bedingter Text". I created a condition named "Temp" with a yellow marker before I configured the Change with GREP:

 

FindChange-1.PNG

 

[2] Second GREP Find/Change:

Find:

[,.:](?=\d[,.:]\x{0020})

plus that special formatting, the condition named "Temp" from above.

 

Change: [No entry]

No formatting.

 

The result would be the text frame at the bottom of this screenshot:

FindChange-2.PNG

 

 

[3] Get rid of that applied special condition "Temp" from the Conditional Text feature.

Find condition named "Temp".

Change to no condition.

 

FindChange-3.PNG

 

Regards,
Uwe Laubender

( ACP )

 

PS: I know, that Barb answered while I am writing this; nevertheless I will post my suggestion; perhaps you can take out the one or the other idea from it.

Likes

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
Advisor ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

I like Simplicity: just 1 click!

 

(^/)

 

/*
    _FRIdNGE-0713_InvertText.jsx
    by FRIdNGE, Michel Allio (^/) The Jedi [31/05/2021]
*/

// .1 ==> 1.
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "[.,;:]\\d+";
myFound = app.activeDocument.findGrep();
for ( var f = 0 ; f < myFound.length ; f++ ) myFound[f].characters[0].move(LocationOptions.after, myFound[f]);
app.findGrepPreferences = null;

/*
// 1. ==> .1
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\d+[.,;:]";
myFound = app.activeDocument.findGrep();
for ( var f = 0 ; f < myFound.length ; f++ ) myFound[f].characters[-1].move(LocationOptions.before, myFound[f]);
app.findGrepPreferences = null;
*/

alert( "Done! … [" + myFound.length + "]" + "\r\rby FRIdNGE, Michel Allio (^/) The Jedi [31/05/2021]" )

Likes

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 ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

I've marked this as the correct answer, but a big THANK YOU to ALL, because all the answers are correct! (and its great to know all the possible solutions provided by you).

 

This one is fast and easy to implement (just place the script in your scripts panel and that's it), so I think it's the "more correct" of all. Great, simple and beautiful script, FRIdNGE! (and also like that you can comment the sencond part of the script to find/replace the oposite occurence!)

Likes

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 ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

LATEST

And also... if you replace this line:

app.findGrepPreferences.findWhat = "[.,;:]\\d+";

 

To this one (note, I've replaced the \\d for ~F):

app.findGrepPreferences.findWhat = "[.,;:]~F";

 

It will also work with footnotes!!!

Likes

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