Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove blank lines

Status
Not open for further replies.

jdbolt

Programmer
Aug 10, 2005
89
CA
Is there a way to remove the blank lines from a string?

For example


line1
line2
line3


line5

Would return:


line1
line2
line3
line5


I have tried the following:

var textTrimmed = textUntrimmed.responseText.replace(/\n/g, '')

But that seems to remove ALL the lines, so it would return:

line1line2line3line5

Any help would be greatly appeciated.
 
What you need to do is replace two instances of newline with one:
[tt]
var textTrimmed = textUntrimmed.responseText.replace(/\n\n/g, "\n")
[/tt]


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
It'll actually require a few more steps, at least when using a textarea. You have to check for new lines AND carriage returns. Additionally, you need to set up a loop to continually remove them until they are all gone. Here's my example:
Code:
<script language="javascript">
function removeDoubleLine(obj) {
   var str = obj.value;
   while(str.indexOf("\r\n\r\n") >= 0) {
      str = str.replace(/\r\n\r\n/g, "\r\n")      
   }
   obj.value = str;
}
</script>
<textarea id="myTextArea" style="height:400px"></textarea>
<input type="button" value="remove extra lines" onclick="removeDoubleLine(document.getElementById('myTextArea'))">

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Cool thanks for your quick replies I will have a play round with your ideas and get back to you.

I thought another option could be to split the string by:

.split('\n');


Then loop through the array, see if the string is blank if so discard it, otherwise append array to a new string?


 
Ok, from you suggestions I have figured out a way to remove all blank lines and lines with only tabs or spaces on them

// Remove all blank lines (lines with only tabs)
var textTrimmed = textUntrimmed.replace(/\t\r\n/g, '');

//Remove all blank lines (lines with nothing on them)
textTrimmed = textTrimmed.replace(/\r\n\r\n/g, '');

Hope this is helpful to anyone in the future :)
 
This will DEFINITELY complicate things:

I just ran some tests and found that Mozilla and Explorer actually format the text from a textarea differently!

Explorer: Line breaks consist of carriage return AND linefeed (\r\n)

Mozilla: Line breaks consist of linefeed ONLY (\n).

Additionally, in Explorer when you put text into a <pre> node it only seems to use the carriage return to determine a line break. The newline characters show up as spaces.

I'll post a URL so you can see the results of my tests when I finish playing around.

Note: I sure wish more languages could be as intelligent as PERL about line breaks.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Here's the URL so you can see what I mean. Open this URL in IE and Mozilla and look at the differences in the way the results come out.


I also found a couple of other interesting bugs/features (one IE and one Mozilla) that are mentioned in the style sheet comments.)


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top