Search code examples
javascriptregexspacecarriage-return

Remove carriage return and space from a string


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.


Solution

  • Try:

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

    Then:

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

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