How to remove spaces between lines in a string using Javascript

Hi, I have a string that looks like :

string = "Hello it is a test comment

              Hello it is a test comment

             Hello it is a test comment"

NOW I want to remove the white spaces between the sentences and should look like
string = "Hello this is a test comment
              Hello this is a test comment
             Hello this is a test comment"

I used some functions that I googled but nothing seems to be working and I still have White spaces between sentences with in the string.

Thanks
VM
vmandemAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brad HoweDevOps ManagerCommented:
Hi,

What you are looking for is removing carriage returns.

// Strip out all line breaks.
var strSingleLineText = strMultiLineText.replace(
// Replace out the new line character.
new RegExp( "\\n", "g" ),
 

// Put in ... so we can see a visual representation of where
// the new line characters were replaced out.
" ... " 
);
 

// Alert the new single-line text value.
alert( strSingleLineText );

http://www.bennadel.com/blog/161-Ask-Ben-Javascript-Replace-And-Multiple-Lines-Line-Breaks.htm

Cheers,
Hades666
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brad HoweDevOps ManagerCommented:
Actually, This code removes ALL linebreaks.

To get this we may need to open replace back to back linebreaks with 2 linebreak.

//Remove all blank lines
strMultiLineText.replace(/\r\n\r\n/g, ");

Cheers,
Hades666
0
vmandemAuthor Commented:
Hades666

Your first code did removed the spaces but all are appearing in the same line rather line by line

Like string =  "Hello this is a test comment Hello this is a test comment Hello this is a test comment"

but I want
string = "Hello this is a test comment
              Hello this is a test comment
             Hello this is a test comment"

YOUR SECOND SOLUTION DID NOT REMOVE THE SPACES AND I STILL GET THE SAME WHITE SPACES.

Thanks

0
vmandemAuthor Commented:
Hades666

I made a small change to your first solution but I get a single space by removing "..." but does not go to new line.

// Strip out all line breaks.
var strSingleLineText = strMultiLineText.replace(
// Replace out the new line character.
new RegExp( "\\n", "g" ),
 

// Put in ... so we can see a visual representation of where
// the new line characters were replaced out.
"  " 
);
0
Terry WoodsIT GuruCommented:
leakim's answer in:
http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q__26027831.html
seems to work for me.

However, it might not work if there are line feed characters. You could try:
replace(/\r?\n\s+\r?\n/g,"\n")
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.