I'm looking for effective way of multiline whitespace-stripping of text string
It should
- Replace all
\r
with\n
. - Remove any leading and trailing whitespaces in each line.
- Remove empty and only-whitespace lines.
- Replace all whitespace-sequencies in each line with a single space.
- Respect all unicode whitespace characters.
So, for a given string
var string = ' \n\t \r \r \xA0\n <1> \r \n\r\r\n\n <2> \t \t \r \t \r \r <3> \n <a a a a> \r \r \r \r\t \n \n ';
It should return
"<1>\n<2>\n<3>\n<a a a a>"
So far I came up to this:
string
.replace(/[ \f\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+/g, ' ')
.replace(/ ?[\n\r][\n\r ]*/g, '\n')
.replace(/^\n|\n$/g, '')
;
Can you suggest a "better" way?
Please, do not suggest .split().map().join()
s