55

How to remove the last "\n" from a textarea?

1
  • 3
    Just to confirm... do you actually mean '\n' as in the new-line character and not '/n'?
    – user353297
    Jun 23, 2010 at 10:04

6 Answers 6

79

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"

https://regexr.com/4uu1r

2
  • 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... May 23, 2015 at 11:46
  • This is the ✅ answer. Feb 24, 2021 at 21:39
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();
6
  • 9
    If 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, 2010 at 12:58
  • @Andrew , your way changes the cursor position. @John ,your's - moves the text on the begginning of the textarea.
    – user370306
    Jun 21, 2010 at 13:27
  • Oh. Are you trying to remove the trailing line break on a textarea that the user is updating?
    – Andrew
    Jun 21, 2010 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 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, 2010 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.
    – user370306
    Jun 21, 2010 at 16:28
6

$.trim() should do the trick!

It trims whitespace and newline characters from the beginning and ending of the specified string.

1
  • 9
    Nowhere did the Questioner indicate they were using jQuery... or that they wanted to trim anything other than a trailing newline. Jan 22, 2016 at 8:07
2

I personally like lodash for trimming newlines from strings. https://lodash.com/

_.trim(myString);
1
  • 1
    To answer the question, use _.trimRight(myString, '\n'). Do note that this will remove any number of trailing newlines, not just one. Aug 9, 2017 at 19:56
1

Tried to use trim under debug console but it does not work:

text
'Add to Cart\n'
text.trim()
'Add to Cart\n'
1
0

You can do it this way (.*[^\n])\n* Result: https://regex101.com/r/lWKzFy/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.