Skip to main content
Participant
October 14, 2012
Answered

Is it possible to access a word by it's index via GREP?

  • October 14, 2012
  • 1 reply
  • 884 views

I am trying to write a script that underlines a specific word in a document: e.g. "and".

For that there are many "and"s in my text, I am looking for a way to access the "and" I want to underline (word number 12) by it's index via GREP.

Any indeas?

Thanks

This topic has been closed for replies.
Correct answer Trevor:

Hi Meindel

This is what you are looking for

var doc = app.documents[0], myAnds;

app.findTextPreferences = app.changeTextPreferences = null;

app.changeGrepPreferences = app.findGrepPreferences = null;

app.findChangeTextOptions.caseSensitive = 0;

app.findChangeTextOptions.wholeWord = 1;

app.findTextPreferences.findWhat = "and";

myAnds = doc.findText ();

myAnds[11].fillColor = "C=15 M=100 Y=100 K=0"; // To make the 12th and red

I used text instead of Greps but you could use the grep findWhat "\\<[Aa]nd\\>" which is more accurate.

1 reply

Trevor:
Legend
October 14, 2012

Do you mean you want to underline the 12th "and" in the document?

Doesn't sound like it.  Please clarify what you want.

meindel12Author
Participant
October 14, 2012

Thanks for your reply.

Thats exactly what I want to do: is it possible to target / mark only word number 12 using GREP?

Edit: If it is not possible to search via a index (12th word), perhaps the way to go would be to somehow include the word that follows the "and" I want into the seach: but without marking it?

Trevor:
Trevor:Correct answer
Legend
October 14, 2012

Hi Meindel

This is what you are looking for

var doc = app.documents[0], myAnds;

app.findTextPreferences = app.changeTextPreferences = null;

app.changeGrepPreferences = app.findGrepPreferences = null;

app.findChangeTextOptions.caseSensitive = 0;

app.findChangeTextOptions.wholeWord = 1;

app.findTextPreferences.findWhat = "and";

myAnds = doc.findText ();

myAnds[11].fillColor = "C=15 M=100 Y=100 K=0"; // To make the 12th and red

I used text instead of Greps but you could use the grep findWhat "\\<[Aa]nd\\>" which is more accurate.