Yes, since I wasn't sure of your requirements, I added the qualifier when I posted the pattern. Now I see that @drdrang's RegEx pattern is the best for your requirements.
With that said, it never hurts to contribute alternatives since others may see this post in the future and have the need to remove trailing whitespace AND remove all whitespace-only lines.
FYI, I recently shared a macro that is pertinent to this topic: Text Transformation EXAMPLES
JavaScript is not a language I've learned*, but I have been known to borrow JavaScript code from others. I've even made some minor changes when I've felt particularly brave. @ComplexPoint's Markdown Link Tool is one such example.
(*I'm a relatively old chemical/process automation engineer and through the many years we often had to do complex transformations particularly when were were moving a configuration from one control system to another. For the really fun stuff, we used awk, first on HP-UX, later on macOS, and then finally on Windows. Oddly enough, there was a third-party awk compiler available on Windows. It was very good.)
I've added @ComplexPoint's JXA method to the example macro. It will be included with the next update.
Someone, correct me if I'm wrong, but I think one must use KM global variables within JXA. In the image above, you'll see I added the Set Variable to Text and Execute AppleScript to delete the global variable before and after the Execute JavaScript For Automation action, respectively.
Finally, I'd like to point out that there is one subtle difference between @drdrang's RegEx and @ComplexPoint's JXA. The former does not remove leading and trailing whitespace-only lines.