Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

GREP Code not Working in JavaScript

Enthusiast ,
Jun 06, 2020 Jun 06, 2020

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 

Best
Mohammad Hasanin
TOPICS
Scripting
869
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jun 06, 2020 Jun 06, 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

Translate
Advocate ,
Jun 06, 2020 Jun 06, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 06, 2020 Jun 06, 2020

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

 

Best Regards

Mohammad

Best
Mohammad Hasanin
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 06, 2020 Jun 06, 2020
LATEST

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 )

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