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

Remove Trailing Whitespace without removing extra lines

Community Expert ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

InDesign has a buit-in GREP Find/Change Query called Remove Trailing Whitespace which is:

\s+$

It's more powerful than I want. It finds/removes spaces, tabs, etc at the end of paragraphs, but also extra linespaces. Is there a GREP search that will find/remove all the whitespace (spaces, tabs, etc) at the end of a line, but ignore extra linespaces?

Thanks in advance.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor

Views

3.8K

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

LEGEND , Jan 31, 2017 Jan 31, 2017

\h+(?!\n)$

Votes

Translate

Translate
Adobe Employee ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Hi,

Does this link help? it has several grep expressions that can be used.

https://www.rockymountaintraining.com/adobe-indesign-removing-unwanted-spaces-fast/

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
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Thanks, but no. That post refers to the built-in Remove Trailing Whitespace query that I mentioned does not work for my needs.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor

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
Mentor ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

What if simply exclude carriage return?

\s+[^\r]$

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
Mentor ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Your reply disappeared for me...

However, here's "improved" version:

\h+[^\r]$

it works on my side...

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
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

winterm​ Your suggestion of \h+[^\r]$ does also work for me. As always, there are multiple ways to do things with GREP .


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor

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
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

winterm​ I tried your suggestion of \s+[^\r]$ but it was not working as expected.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor

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
Mentor ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Oooh, it was a hard day....

Simple \h+$ is just enough, blind me!

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
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

The \h is nice, I didn't know about that! I had to look it up, so for anyone else that doesn't know:

\h is horizontal whitespace

\v is vertical whitespace (finds both hard and soft returns)

I'd probably use +? instead of + for shortest match. Not sure if it's necessary, but it would be safer, right?

So to break \h+?$ down:

\h = horizontal whitespace

+? = repeat one or more times (shortest match)

$ = at end of paragraph

That works very well and I like the elegance of it, but if there are soft returns, it also removes the space before the soft return.

When using \s*?(?=\r) I found it does not remove the spaces before soft returns, which is a little better for me. So far that looks like the winner for my exact needs.

Thanks everyone for the help! I always enjoy learning something new and hope others can benefit too.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor

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
LEGEND ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

\h+(?!\n)$

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
Mentor ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Yeah, I’m with Obi-wan here. You should use 'horizontal space', not 'any white space', anyway.

Negative Lookahead works nicely. And no need for 'shortest match', IMHO.

Your 'working' regex \s*?(?=\r) finds a lot of false positives to me (well, harmless), just use Find Next to see...

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
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Yes, I agree too. Obi-wan Kenobi​'s is the best so far.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor

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
Explorer ,
Oct 03, 2022 Oct 03, 2022

Copy link to clipboard

Copied

@Obi-wan Kenobi this is perfect for what I want to implement but the problem is when I'm adding this in script my InDesign crash

var doc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

app.findGrepPreferences.findWhat = "\\h+(?!\\n)$";
app.changeGrepPreferences.changeTo = "";
app.activeDocument.changeGrep();

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

 

NewInScript_0-1664782958429.png

 

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
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

I'm not sure why the reply didn't get posted here, but I got an email answer that worked:

[[:space:]]*?(?=\r)

I shortened that to the following, which also is working for me:

\s*?(?=\r)


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor

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
Community Expert ,
Oct 03, 2022 Oct 03, 2022

Copy link to clipboard

Copied

@Shanpub said: "…this is perfect for what I want to implement but the problem is when I'm adding this in script my InDesign crash"

 

If a GREP expression like

"\\h+(?!\\n)$"

is crashing InDesign, it could be a bug in a specific version of InDesign or a strange condition in your document you are running the code at. To test this just run your code to this point:

var doc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "\\h+(?!\\n)$";
app.changeGrepPreferences.changeTo = "";

 

Then open the GUI for GREP Find/Change and check if you see the right expression in the GUI.

Test if the GUI can handle the Find/Change action on the active document.

 

If all goes well with that, return to your code and run it not on the document, but on a selection of text.

If InDesign will crash then, just provide a sample document where you see the issue. Simply attach the document to your reply post.

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

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
Community Expert ,
Oct 03, 2022 Oct 03, 2022

Copy link to clipboard

Copied

Why would anyone wants trailing lines or paragraphs if correct paragraph styles are used?

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
Community Expert ,
Oct 03, 2022 Oct 03, 2022

Copy link to clipboard

Copied

LATEST

100% agree, but I do find very selective use of line returns useful (such as dividing a long heading without creating yet another style that folds it neatly).

 

It does sound as if the original OP, long ago, had used them much more liberally, which is indeed a practice to avoid.

 

—


â•Ÿ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) â•¢

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