JavaScript: how to use a regular expression to remove blank lines from a string?

JavascriptRegexBlank Line

Javascript Problem Overview


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!

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

I believe this will work

searchText.replace(/(^[ \t]*\n)/gm, "")

Solution 3 - Javascript

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.

Solution 4 - Javascript

Here's another solution:

string = string.replace(/^(?=\n)$|^\s*|\s*$|\n\n+/gm, "")

It appears to handle both blank lines and space-only lines.

Solution 5 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncurious1View Question on Stackoverflow
Solution 1 - JavascriptMartin EnderView Answer on Stackoverflow
Solution 2 - JavascriptaaronmanView Answer on Stackoverflow
Solution 3 - JavascriptBjørn BråthenView Answer on Stackoverflow
Solution 4 - JavascriptPikamander2View Answer on Stackoverflow
Solution 5 - Javascriptman tou View Answer on Stackoverflow