Skip to main content
Inspiring
January 29, 2024
Answered

Grep to remove space before superscript number

  • January 29, 2024
  • 3 replies
  • 754 views

Can anyone tell me the GREP for searcing for a normal space before a superscript number and removing it. I already have one to search for space before endnote reference and delete \s(?=~F) but in my latest document all the note markers have been done manually as a superscript number with a space before, not as an endnote marker so I need to try and remove the space without doing it manually as there are over 400 of them.

 

Thanks

This topic has been closed for replies.
Correct answer Prithivirajan29008824dh9b

Check below code

 

app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.position = Position.SUPERSCRIPT;
var myResults = app.activeDocument.findText();
var myfind = app.findText()
for(i=0;i<myfind.length;i++)
{
app.select(myResults[i]);
var myIndex = app.selection[0].index-1
var myprevcharacter = app.selection[0].parentStory.characters[myIndex];
if (myprevcharacter.contents==" ")
myprevcharacter.remove()
}

3 replies

James Gifford—NitroPress
Brainiac
January 29, 2024

NM

New Participant
January 29, 2024

Check below code

 

app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.position = Position.SUPERSCRIPT;
var myResults = app.activeDocument.findText();
var myfind = app.findText()
for(i=0;i<myfind.length;i++)
{
app.select(myResults[i]);
var myIndex = app.selection[0].index-1
var myprevcharacter = app.selection[0].parentStory.characters[myIndex];
if (myprevcharacter.contents==" ")
myprevcharacter.remove()
}
Community Expert
January 30, 2024

The script works, but there's no need to select what you found: just reference it as is. If there's no need to select things, don't: selections tend to be unstable and slow down things considerably.

New Participant
January 30, 2024

Thank Perer for pointing out.

 

app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.position = Position.SUPERSCRIPT;
var myResults = app.activeDocument.findText();
var myfind = app.activeDocument.findText()
for(i=0;i<myfind.length;i++)
{
var myIndex = myResults[i].index-1
var myprevcharacter = myResults[i].parentStory.characters[myIndex];
if (myprevcharacter.contents==" ")
myprevcharacter.remove()
}

danaken3
Participating Frequently
January 29, 2024

Here's a workaround:

  1.  Find all superscript numbers, and add a sequence of symbols in front of them that is definitely not found anywhere else in your document—for example, three percentage signs in a row. (Note the $0 in the "Change to" field represents the found number itself.)
  2.  Then find all instances of a space followed by three percentage signs (make sure to clear the "Find Format" field since the space itself is not superscript). Replace with nothing.
danaken3
Participating Frequently
January 29, 2024

Actually I'd probably change that second GREP search to:

\s*%%%

(zero or more spaces, followed by 3 percentage signs). That way, if you have any endnote markers that do not actually have a space preceding them, you won't be left with random percentage signs all over your document. And it will also get rid of any double spaces if there are any. (It would be a good idea to search for triple percentage signs at the end of all this to make sure nothing was missed.)