Remove the last “\n” from a textarea

℡╲_俬逩灬. 提交于 2020-01-22 13:06:30

问题


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


回答1:


verses ="1\n222\n"
verses = verses.replace(/\n$/, "")
-> returns "1\n222"



回答2:


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();



回答3:


$.trim() should do the trick!

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




回答4:


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

_.trim(myString);


来源:https://stackoverflow.com/questions/3084708/remove-the-last-n-from-a-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!