Reputation: 1076
Actual:
A new line begins
Another line begins
Here's another
Expected:
A new line begins
Another line begins
Here's another
So far I have tried this which removes all the leading spaces before word after line breaks:
var regex:RegExp = /(\r?\n|\r)+(\s+|\s+$)/g;
var newText:String = abcd.replace(regex, "\n");
Alert.show(StringUtil.trim(newText));
But I'm having difficulty to set a condition to leave blank lines as they are.
Upvotes: 2
Views: 232
Reputation: 138017
A simple option is to match and remove only the spaces at the start of lines, and never newlines:
var regex:RegExp = /^[ \t]+/gm;
var newText:String = abcd.replace(regex, "");
/m
(multiline) flag, so ^
matches at the beginning of every line.^[ \t]+(?=\S)
.Working example: https://regex101.com/r/gdMZLZ/2
Upvotes: 2