Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 274
Guest Guest Avatar asked Apr 07 '14 19:04

Guest Guest


People also ask

How do you remove spaces from a string in JavaScript?

JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.

How do I remove a carriage return from a string in Java?

replaceAll("\\n", ""); s = s. replaceAll("\\r", ""); But this will remove all newlines. Note the double \ 's: so that the string that is passed to the regular expression parser is \n .

Does trim remove line breaks?

trim method removes any line breaks from the start and end of a string. It handles all line terminator characters (LF, CR, etc). The method also removes any leading or trailing spaces or tabs. The trim() method does not change the original string, it returns a new string.

How do I remove a carriage return from a string?

In the Find box hold down the Alt key and type 0 1 0 for the line feed and Alt 0 1 3 for the carriage return. They can now be replaced with whatever you want.


1 Answers

Try:

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

Then:

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

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

like image 76
Andrew Newby Avatar answered Oct 01 '22 12:10

Andrew Newby