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.
JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.
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 .
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.
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.
Try:
t.replace(/[\n\r]+/g, '');
Then:
t.replace(/\s{2,10}/g, ' ');
The 2nd one should get rid of more than 1 space
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With