Skip to main content
davecourtemanche
Inspiring
June 16, 2025
Question

GREP expression needed

  • June 16, 2025
  • 1 reply
  • 127 views

I have a character style "nocolor" that I would like to apply to commas that fall at the end of a line, in the middle of a description (client does not like commas at the end of lines, but when copy is reflowed, we have to remember to add the commas back in). Is there a GREP expression that will capture the comma at the end of a line (not paragraph) and apply the character style to it? (in attached example, the comma after "sugar".

1 reply

Peter Kahrel
Community Expert
Community Expert
June 17, 2025

There's no GREP expression to match the end of a line of text in InDesign, only to match the end of a paragraph. You'll need a script for that. That script can undo undo any existing instances of the hiding character style before applying the style (again).

 

Then select a text frame or click somewhere in the text. The script targets the selected story only.

 

style = app.activeDocument.characterStyles.item ('nocolor');
story = app.selection[0].parentStory;

// Remove any existing instances of the style in the selected story

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = ',';
app.findGrepPreferences.appliedCharacterStyle = style;
app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyles[0];
story.changeGrep();

// Apply the style to line-final commas

ch = story.lines.everyItem().characters[-2].getElements();
for (i = ch.length-2; i >= 0; i--) {
  if (ch[i].contents === ',') {
    ch[i].appliedCharacterStyle = style;
  }
}