JavaScript - Remove blank lines


JavaScript - Remove blank lines. This program removes blank lines.

CODE
<!--JQuery Reference Library--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <script> function removeBlankLines(){ inputText = "Apple\n\n\nBanana\nOrange"; // Provide the input text here. var lines = inputText.split("\n"); //Split the input based on the new line character. var resultArray = []; //Loop through the splitted lines. $.each(lines, function(){ if(String(this).trim() != "") resultArray.push(String(this)); //Check if the line is blank. If blank, do not insert it into the array, otherwise insert. }); var result = resultArray.join("\r\n"); //Join all the array elements with a new line character. alert(result); } removeBlankLines(); //Output //Apple //Banana //Orange </script>

Comments