76

I need to use JavaScript to remove blank lines in a HTML text box. The blank lines can be at anywhere in the 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:

/^\s*\r?\n/g   

/^\s*\r?\n$/g

Edit 1

It appears that the solution (I modified it a little) suggested by aaronman and m.buettner works:

string.replace(/^\s*\n/gm, "") 

Can someone tell why my first regular expression is not working?

Edit 2

After reading all useful answers, I came up with this:

/^[\s\t]*(\r\n|\n|\r)/gm

Is this going to be one that cover all situations?

Edit 3

This is the most concise one covering all spaces (white spaces, tabs) and platforms (Linux, Windows, Mac).

/^\s*[\r\n]/gm

Many thanks to m.buettner!

Jaumzera
  • 2,305
  • 1
  • 30
  • 44
curious1
  • 14,155
  • 37
  • 130
  • 231
  • 4
    `I am expecting a regular expression solution to this.` Eat healthy and post the RegExp here if it doesn't work – Ejaz May 04 '13 at 01:18
  • 6
    Tag in title: check, No code: check, Wrong attitude: check, Ironic thanks message: check, Downvote: check. – elclanrs May 04 '13 at 01:19
  • Even if you're new to this site you can search it for something like regex blank lines or something. You can see from example what will be answered and what will be voted right off the map... for example: http://stackoverflow.com/questions/3012788/how-to-check-if-a-line-is-blank-using-regex - that answers your needs – Kai Qing May 04 '13 at 01:20
  • Hello folks, I did try a number regular expressions myself, but simply cannot get it done. And I googled for it. Try my title in google search and show me where you get the answer. – curious1 May 04 '13 at 01:21
  • 1
    right, so update your question with code you tried. we will show you what you did wrong. The way it is it looks like you want us to do your work for you – Kai Qing May 04 '13 at 01:21
  • I would do it like this: 'Line1\n\nLine3'.split(/\r?\n/).filter(Boolean).join('\n') – kalicki2k Feb 18 '20 at 08:08

5 Answers5

106

Your pattern seems alright, you just need to include the multiline modifier m, so that ^ and $ match line beginnings and endings as well:

/^\s*\n/gm

Without the m, the anchors only match string-beginnings and endings.

Note that you miss out on UNIX-style line endings (only \r). This would help in that case:

/^\s*[\r\n]/gm

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 \s*.

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:

/^\s*$(?:\r\n?|\n)/gm
Community
  • 1
  • 1
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • m.buettner, thanks so much for pointing out the Mac scenario. – curious1 May 04 '13 at 01:39
  • m.buettner, I created a new one /^[\s\t]*(\r\n|\n|\r)/gm. Is it going to cover all situations? – curious1 May 04 '13 at 02:05
  • @curious1, \s includes all white space. That is spaces, \t, \r and \n. That was the point of using \s in the first place. Therefore, our patterns do exactly the same, and yes, they both cover all cases. – Martin Ender May 04 '13 at 09:12
  • m.buettner, thanks so much for the detailed and crystal-clear explanation. best. – curious1 May 04 '13 at 15:59
  • 3
    This will fail if the last line is blank because there is no carriage return, probably the most common case. Needs a question mark. `/^\s*[\r\n]?/gm` – Dex Feb 28 '18 at 21:34
  • @Dex that doesn't work. It would remove leading whitespace from all lines. You'd have to insert a `$` in front of the character class. It's a good catch though, I'll edit the answer. – Martin Ender Feb 28 '18 at 21:42
  • Strings with \r\n as newline will not work: `"a\r\nb".replace(/^\s*$(?:\r\n?|\n)/gm, '')==="ab"` – man tou Aug 29 '20 at 06:22
25

I believe this will work

searchText.replace(/(^[ \t]*\n)/gm, "")
aaronman
  • 18,343
  • 7
  • 63
  • 78
7

This should do the trick i think:

var el = document.getElementsByName("nameOfTextBox")[0];
el.value.replace(/(\r\n|\n|\r)/gm, "");

EDIT: Removes three types of line breaks.

Bjørn Bråthen
  • 410
  • 8
  • 20
4

Here's a reusable function that will trim each line's whitespace and remove any blank or space-only lines:

function trim_and_remove_blank_lines(string)
{
    return string.replace(/^(?=\n)$|^\s*|\s*$|\n\n+/gm, "")
}

Usage example:

trim_and_remove_blank_lines("Line 1 \nLine2\r\n\r\nLine4\n")

//Returns 'Line 1\nLine2\nLine4'
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
-1
function removeEmptyLine(text) {
  return text.replace(/(\r?\n)\s*\1+/g, '$1');
}

test:

console.assert(removeEmptyLine('a\r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n\r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n \r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n \r\n  \r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n \r\n 2\r\n  \r\nb') === 'a\r\n 2\r\nb');
console.assert(removeEmptyLine('a\nb') === 'a\nb');
console.assert(removeEmptyLine('a\n\nb') === 'a\nb');
console.assert(removeEmptyLine('a\n \nb') === 'a\nb');
console.assert(removeEmptyLine('a\n \n  \nb') === 'a\nb');
console.assert(removeEmptyLine('a\n \n2 \n  \nb') === 'a\n2 \nb');
man tou
  • 129
  • 5