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.
\h+(?!\n)$
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/
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.
Copy link to clipboard
Copied
What if simply exclude carriage return?
\s+[^\r]$
Copy link to clipboard
Copied
Your reply disappeared for me...
However, here's "improved" version:
\h+[^\r]$
it works on my side...
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 .
Copy link to clipboard
Copied
winterm​ I tried your suggestion of \s+[^\r]$ but it was not working as expected.
Copy link to clipboard
Copied
Oooh, it was a hard day....
Simple \h+$ is just enough, blind me!
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.
Copy link to clipboard
Copied
\h+(?!\n)$
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...
Copy link to clipboard
Copied
Yes, I agree too. Obi-wan Kenobi​'s is the best so far.
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;
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)
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 )
Copy link to clipboard
Copied
Why would anyone wants trailing lines or paragraphs if correct paragraph styles are used?
Copy link to clipboard
Copied
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.
—