textarea element. A blank line can be just a return or white spaces plus return.
I am expecting a regular expression solution to this. Here are some I tried, but they are not working and cannot figure out why:
It appears that the solution (I modified it a little) suggested by aaronman and m.buettner works:
Can someone tell why my first regular expression is not working?
After reading all useful answers, I came up with this:
Is this going to be one that cover all situations?
This is the most concise one covering all spaces (white spaces, tabs) and platforms (Linux, Windows, Mac).
Many thanks to m.buettner!
Your pattern seems alright, you just need to include the multiline modifier
m, so that
$ match line beginnings and endings as well:
m, the anchors only match string-beginnings and endings.
Note that you miss out on Mac line endings (only
\r). This would help in that case:
Also note that (in both cases) you don't need to match the optional
\r in front of the
\n explicitly, because that is taken care of by
As Dex pointed out in a comment, this will fail to clear the last line if it consists only of spaces (and there is no newline after it). A way to fix that would be to make the actual newline optional but include an end-of-line anchor before it. In this case you do have to match the line ending properly though:
I believe this will work
searchText.replace(/(^[ \t]*\n)/gm, "")
This should do the trick i think:
var el = document.getElementsByName("nameOfTextBox"); el.value.replace(/(\r\n|\n|\r)/gm, "");
EDIT: Removes three types of line breaks.
©2020 All rights reserved.