Remove carriage return and space from a string

Guest Guest picture Guest Guest · Apr 7, 2014 · Viewed 50.9k times · Source

I want to remove carriage return and space from a string for exemple:

var t ="     \n \n    aaa \n bbb \n ccc \n";

I want to have as result:

t = "aaa bbb ccc"

I use this one, it removes carriage return but I still have spaces

t.replace(/[\n\r]/g, '');

Please someone help me.

Answer

Andrew Newby picture Andrew Newby · Apr 7, 2014

Try:

 t.replace(/[\n\r]+/g, '');

Then:

 t.replace(/\s{2,10}/g, ' ');

The 2nd one should get rid of more than 1 space