Highlighted

GREP Code not Working in JavaScript

Engaged ,
Jun 06, 2020

Copy link to clipboard

Copied

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 

Adobe Community Professional
Correct answer by Sunil_Yadav1 | Adobe Community Professional

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

Topics

Scripting

Views

354

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

GREP Code not Working in JavaScript

Engaged ,
Jun 06, 2020

Copy link to clipboard

Copied

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 

Adobe Community Professional
Correct answer by Sunil_Yadav1 | Adobe Community Professional

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

Topics

Scripting

Views

355

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 06, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Engaged ,
Jun 06, 2020

Copy link to clipboard

Copied

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

 

Best Regards

Mohammad

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 06, 2020

Copy link to clipboard

Copied

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 )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...