Skip to main content
gopaljay78
Known Participant
February 5, 2010
Question

How to get substring from Indesign grep search

  • February 5, 2010
  • 2 replies
  • 922 views

Hi,

Can anybody tell how to extract substring from grep search.

For example, here is my JS code:

    app.findGrepPreferences = null;
    app.findGrepPreferences.findWhat = "\\{\\{(artref|boxref|tableref) (.+)( ?)(.+)?\\}\\}";

    found = app.activeDocument.findGrep ();

    for (i = 0; i<found.length; i++)
    {

          here I want to save the string   "$2" in to some variable.

     }

From the above script, I want to  save second pattern match ($2 value) to  another  variable.

Thanks,

Gopal

This topic has been closed for replies.

2 replies

Inspiring
February 5, 2010

Hi Gopal,

Please see below code:

var myDoc =  app.activeDocument;

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "\\{\\{(artref|boxref|tableref) (.+)( ?)(.+)?\\}\\}";

found = app.activeDocument.findGrep ();

alert (found.length)

var new_grep = new Array()

for(a=0; a<found.length; a++)

{

new_grep = found.contents.replace(/[{,}, ,artref,boxref,tableref]/g,"")

}

Shonky

Inspiring
February 5, 2010

Hi Gopal,

With below code you can easily store your data in array.

Hope it will help you.

var myDoc =  app.activeDocument;

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "(?<=\\{\\{(artref|boxref|tableref) )(.+)( ?)(.+)?(?=\\}\\})";

found = app.activeDocument.findGrep ();

alert (found.length)

Cheers,

Shonky

gopaljay78
Known Participant
February 5, 2010

Hi Shonkyin,

Thanks for your help.

This script works well. But I want to save the second pattern mathch (.*) value to some variable for later use.

Is it possible to do so.

Thanks,

Gopal