Skip to main content
Participant
February 27, 2024
Answered

GREP search for space before first instance of a character style

  • February 27, 2024
  • 2 replies
  • 434 views

I have a book with articles written by authors and each ends with the author name which is character styled. But I want to change the space just before the author name to a right-indent tab.

I've already done a search and replace for the spaces between the author name to a non-breaking space. 

 

Is there a way to search with GREP for the space just before the first instance of the author name character style?

See screenshot attached.

This topic has been closed for replies.
Correct answer Eugene Tyson

Can you find the character style

 

GREP

 

Find

(.+)

Change to

<right indent tab>$0

Find Style
Character Style

 

Afterwards you could do a find/change for 

<space><right indent tab>

Change to

<right indent tab>

 

2 replies

Eugene TysonCommunity ExpertCorrect answer
Community Expert
February 28, 2024

Can you find the character style

 

GREP

 

Find

(.+)

Change to

<right indent tab>$0

Find Style
Character Style

 

Afterwards you could do a find/change for 

<space><right indent tab>

Change to

<right indent tab>

 

Participant
February 28, 2024

Yep, that worked! Thanks so much!

It leaves the space and then right indent tab, but I can easily search for that and replace with just the right indent tab.

 

Thanks again!

Community Expert
February 28, 2024

I don't know if I understood your ask properly but if it is always a space before the text then you could apply the character style to the space as well and then run the Grep to find the text with the character style applied. The first character of your result would be the space character you wanted. Now if we can't or won't apply the character style to the space character then I don't think we can find it by just using a Grep query. We would need the help of script. Check out the following script

 

app.findTextPreferences = null
app.findTextPreferences.appliedCharacterStyle = "CS1"
var r = app.documents[0].findText()
for(var i = 0 ; i < r.length; i++){
    var sb = r[i].parentStory.characters[r[i].index - 1]
    sb.contents = SpecialCharacters.RIGHT_INDENT_TAB
}
app.findTextPreferences = null

Edit :- Change CS1 with the name of your style in the code above.

 

 

-Manan

-Manan