Highlighted

Using GREP and Javascript to replace 2 specific spaces with TABs

New Here ,
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! 

Adobe Community Professional
Correct answer by Peter Kahrel | Adobe Community Professional

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.

 

TOPICS
Scripting

Views

66

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

Using GREP and Javascript to replace 2 specific spaces with TABs

New Here ,
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! 

Adobe Community Professional
Correct answer by Peter Kahrel | Adobe Community Professional

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.

 

TOPICS
Scripting

Views

67

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
Sep 16, 2020 0
Adobe Community Professional ,
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.

 

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...
Sep 16, 2020 2
Alecvdv LATEST
New Here ,
Sep 16, 2020

Copy link to clipboard

Copied

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

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...
Sep 16, 2020 0