I need to trim leading and trailing spaces from a multi-line string. I tried this regex using the String replacement method:
String.replace(/^\s+|\s+$/gm, "");
However, only in lines with spaces, line breaks are lost in the process. For example, (^ stands for space):
^^^^1234^^^^ ^^^^5678^^^^ ^^^^^^^ ^^90^^
outputs this:
1234 5678 90
I want to use regex to save the third (empty) line:
1234 5678 90
source share