How to remove the last "\n" from a textarea?
Asked
Active
Viewed 4.7k times
52
-
3Just to confirm... do you actually mean '\n' as in the new-line character and not '/n'? – user353297 Jun 23 '10 at 10:04
6 Answers
76
Only remove the last newline characters (\n
):
verses1 = "1\n222\n"
verses1.replace(/\n$/, "")
// "1\n222"
verses2 = "1\n222\n\n"
verses2.replace(/\n$/, "")
// "1\n222\n"
Only all the last newlines (\n
):
verses = "1\n222\n\n"
verses.replace(/\n+$/, "")
// "1\n222"

Gianfranco P.
- 10,049
- 6
- 51
- 68

aarti
- 2,815
- 1
- 23
- 31
-
I like this the most. In a stream of `stdout` for example it will have a huge performance hit though. But there is no way around in this case... – eljefedelrodeodeljefe May 23 '15 at 11:46
-
13
from http://en.wikipedia.org/wiki/Trim_%28programming%29
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
};
That will add a trim function to string variables that will remove whitespace from the beginning and end of a string.
With this you can do stuff like:
var mystr = "this is my string "; // including newlines
mystr = mystr.trim();

John Boker
- 82,559
- 17
- 97
- 130
-
9If all @lam3r4370 wants to do is replace one trailing `\n` character, replace `/^\s+|\s+$/g` above with `/\n$/`. Also, if you're planning on distributing this code or putting it where it is likely to share space with JS from other users, I'd recommend you just create a generic function `trim` rather than using `String.prototype`. Though it's very common practice, you can cause trouble for others by adding methods to core JS objects. – Andrew Jun 21 '10 at 12:58
-
@Andrew , your way changes the cursor position. @John ,your's - moves the text on the begginning of the textarea. – Jun 21 '10 at 13:27
-
Oh. Are you trying to remove the trailing line break on a textarea that the user is updating? – Andrew Jun 21 '10 at 14:59
-
I guess it's obvious you are trying to update the text area without changing where the user's cursor is. Check out [this SO post](http://stackoverflow.com/questions/263743/how-to-get-cursor-position-in-textarea) about getting the cursor position. It's a messy process that requires some browser-specific hacks. Read the cursor position; remove the trailing white space; set the cursor position to what it was before. Are you sure you need to update the text area while the user is still in it? I'd be surprised if you couldn't leave the textarea as is and trim the text before you use it. – Andrew Jun 21 '10 at 15:14
-
@Andrew ,I am trying to learn javascript and decided to make a online code editor only for my own use(to "upgrade" my knowledge).I think that is the answer of your question. – Jun 21 '10 at 16:28
-
5
$.trim() should do the trick!
It trims whitespace and newline characters from the beginning and ending of the specified string.

Jesse
- 8,605
- 7
- 47
- 57

Ben Taliadoros
- 7,003
- 15
- 60
- 97
-
8Nowhere did the Questioner indicate they were using jQuery... or that they wanted to trim anything other than a trailing newline. – John Hascall Jan 22 '16 at 08:07
2
I personally like lodash for trimming newlines from strings. https://lodash.com/
_.trim(myString);

creftos
- 2,090
- 1
- 18
- 22
-
1To answer the question, use `_.trimRight(myString, '\n')`. Do note that this will remove *any* number of trailing newlines, not just one. – Resigned June 2023 Aug 09 '17 at 19:56
0
Tried to use trim under debug console but it does not work:
text
'Add to Cart\n'
text.trim()
'Add to Cart\n'

Jue
- 31
- 7
-
This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31904382) – Benoît Fraikin Jun 03 '22 at 17:51