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

GREP expression needed

Engaged ,
Jun 16, 2025 Jun 16, 2025

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".Screenshot 2025-06-16 at 12.10.27 PM.png

TOPICS
How to , Type
110
Translate
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
Community Expert ,
Jun 17, 2025 Jun 17, 2025
LATEST

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

 

Translate
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