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
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();
$.trim() should do the trick!
It trims whitespace and newline characters from the beginning and ending of the specified string.
I personally like lodash for trimming newlines from strings. https://lodash.com/
_.trim(myString);
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