Skip to main content
M.Hasanin
Inspiring
June 6, 2020
Answered

GREP Code not Working in JavaScript

  • June 6, 2020
  • 2 replies
  • 1059 views

Dear Professionals ..

I wrote very simple code to search for any numbers and put them into brackets and the GREP code is working in Find/Change but not in javascript at all and no errors

app.findGrepPreferences=app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat="(\d+)";
app.changeGrepPreferences.changeTo="($1)";
app.activeDocument.changeGrep();

So Whats Wrong i  do? 

Best Regards 

This topic has been closed for replies.
Correct answer Sunil Yadav

Hi medos20,

Obviously your grep will not find digit because in JavaScript String "(\d+)" will act like "(d+)" because first slace will act like escape character, So you can change your grep to "(\\d+)".

Try this code sample:

 

app.findGrepPreferences=app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat="(\\d+)";
app.changeGrepPreferences.changeTo="($1)";
app.activeDocument.changeGrep();

 

 

Best

Sunil

2 replies

Community Expert
June 6, 2020

Hi Mohammad,

there is a simple way to debug your GREP expression. Just run e.g. this code on InDesign:

app.findGrepPreferences.findWhat="(\d+)";

Then go to InDesign and open the GREP Find/Change panel in the GUI.

You'll see that in the find field there is just d+ visible and not the pattern you want: \d+.

 

Regards,
Uwe Laubender

( ACP )

Sunil Yadav
Sunil YadavCorrect answer
Legend
June 6, 2020

Hi medos20,

Obviously your grep will not find digit because in JavaScript String "(\d+)" will act like "(d+)" because first slace will act like escape character, So you can change your grep to "(\\d+)".

Try this code sample:

 

app.findGrepPreferences=app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat="(\\d+)";
app.changeGrepPreferences.changeTo="($1)";
app.activeDocument.changeGrep();

 

 

Best

Sunil

M.Hasanin
M.HasaninAuthor
Inspiring
June 6, 2020

Thank you very much sunail, you saved me..Thanks again

 

Best Regards

Mohammad

Mohammad Hasanin