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!

How to remove empty lines from a string?

Status
Not open for further replies.

Maim

Programmer
Jun 25, 1999
106
CA
I have a string object as follows
"abc

def

ghi

jkl

mno"

I would like to transform it into
"abc
def
ghi
jkl
mno"

I'm not familiar with regular expression pattern matching so I'm asking here.

I tried something like
Code:
text = text.split(/^\r\n/).join('');
but that doesn't do anything.

Any help is appreciated.

-----------------------------------
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rich Cook
 
To get rid of all blank lines, no matter how many there are in a row (2, 3, 4, etc.), you can use (I know it's not regular expressions):

while (text.indexOf('\n\n')
{
text=text.replace('\n\n', '\n');
}

Lee
 
Is there a chance that there will be blank spaces on the lines as well? Whether or not, this should work:
Code:
var myRe = /^[ \r\n]+$/gi;
theString = theString.replace(myRe,"");
I put the space, carriage-return and line-feed in a character class just in case you get a string that uses one, but not the other (i.e unix or mac).


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I missed a final > -1) at the end of the first line:

while (text.indexOf('\n\n') > -1)
{
text=text.replace('\n\n', '\n');
}

 
trollacious: Thanks for the suggestion, however '\n\n' isn't enough, using '\r\n\r\n' and replacing with '\r\n' does the job

tsdragon: No the lines I want removed are completely empty, if there's a space, then leave it. Uh, and the code didn't seem to work. Maybe because no spaces?

Thanks guys

-----------------------------------
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rich Cook
 
Of course is doesn't work, and I'm an idiot. JS doesn't have "multiline matching" like perl does. MSDN says the ^ and $ match at the end of the line or string. I'm assuming in this case they only match at the end of the string, so of course unless you have nothing but space, CR and LF it won't match. Take out the ^ and $ and it works. Oh, you might want to replace CR and LF with space instead of nothing. Run my example and you'll see why.
Here's the code I tested with:
Code:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<b>before replacing:</b>
<hr>
<pre id=o1></pre>
<hr>
<br>
<b>after replacing:</b>
<hr>
<pre id=o2></pre>
<hr>
<br>
</BODY>
<script language="JavaScript">
var aStr = "Tracy\n\n\nDryden\n  \n\rtsdragon\n \r\nme";
var myre = /[\r\n]+/gi;
var bStr = aStr.replace(myre,"");
document.getElementById('o1').innerText = aStr;
document.getElementById('o2').innerText = bStr;
</script>
</HTML>

See my new posting under "Important Lesson" here too. It's very relevant to this thread.

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