Say I have this single string, here I denote spaces (\" \") with ^
^^quick^^^\\n
^brown^^^\\n
^^fox^^^^^\\n
What regular expression to use
Add the 'm'
multi-line modifier.
replace(/\s+$/gm, "")
Or faster still...
replace(/\s\s*$/gm, "")
Why is this faster? See: Faster JavaScript Trim
Addendum: The above expression has the potentially unwanted effect of compressing adjacent newlines. If this is not the desired behavior then the following pattern is preferred:
replace(/[^\S\r\n]+$/gm, "")
Edited 2013-11-17: - Added alternative pattern which does not compress consecutive newlines. (Thanks to dalgard for pointing this deficiency out.)