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

Using GREP and Javascript to replace 2 specific spaces with TABs

New Here ,
Sep 16, 2020 Sep 16, 2020

Copy link to clipboard

Copied

Hi all,

I am trying to replace 2 specific spaces on either side of a 7 digit number with TAB spaces, mostly so I can apply my paragraph style to it. I am trying to also get it to apply only to the selected text frame.

 

Screen Shot 2020-09-16 at 5.29.23 pm.png

app.findGrepPreferences.findWhat = "(?<=\d{7})\s"; //Spaces on either side of the 7 digit number
app.changeGrepPreferences.changeTo = "\t"; //replace with tabs instead of spaces
app.selection[0].paragraphs[0].changeGrep();

At this stage im not sure if it's the Javascript or the GREP that's not functioning as i want. It could be both. 
Any guidance would be amazing! 

TOPICS
Scripting

Views

295

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
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 16, 2020 Sep 16, 2020

Try this:

 

app.findGrepPreferences.findWhat = "\\s(\\d{7})\\s"; //Spaces on either side of the 7 digit number
app.changeGrepPreferences.changeTo = "\t$1\t"; //replace with tabs instead of spaces
app.selection[0].parentStory.changeGrep();

- You need to escape backslashes when you use them is strings (\\s, not \s)

- To target a textframe's contents, select it and do app.selection[0].parentStory.

 

P.

 

Votes

Translate

Translate
Community Expert ,
Sep 16, 2020 Sep 16, 2020

Copy link to clipboard

Copied

Try this:

 

app.findGrepPreferences.findWhat = "\\s(\\d{7})\\s"; //Spaces on either side of the 7 digit number
app.changeGrepPreferences.changeTo = "\t$1\t"; //replace with tabs instead of spaces
app.selection[0].parentStory.changeGrep();

- You need to escape backslashes when you use them is strings (\\s, not \s)

- To target a textframe's contents, select it and do app.selection[0].parentStory.

 

P.

 

Votes

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
community guidelines
New Here ,
Sep 16, 2020 Sep 16, 2020

Copy link to clipboard

Copied

LATEST

Thank you so much Peter, it works perfectly! Lesson learned.

Votes

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
community guidelines