[ACCEPTED]-Trim trailing spaces before newlines in a single multi-line string in JavaScript-regex
Accepted answer
Add the 'm'
multi-line modifier.
replace(/\s+$/gm, "")
Or faster still...
replace(/\s\s*$/gm, "")
Why 7 is this faster? See: Faster JavaScript Trim
Addendum: The above expression 6 has the potentially unwanted effect of compressing 5 adjacent newlines. If this is not the desired 4 behavior then the following pattern is preferred:
replace(/[^\S\r\n]+$/gm, "")
Edited 2013-11-17: - Added 3 alternative pattern which does not compress 2 consecutive newlines. (Thanks to dalgard 1 for pointing this deficiency out.)
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.