Javascript - Trim all spaces


Javascript - Trim all spaces. This program trim spaces including the in between spaces from each of the lines.

CODE
<!--JQuery Reference Library--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <script> function trimAllSpaces(){ inputText = "This is line 1\nThis is line 2\nThis is line 3"; // 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(){ resultArray.push(String(this).replace(/\s+/g, '')); //Trim all the spaces, including the in between spaces and add it to the result array. }); var result = resultArray.join("\r\n"); //Join all the array elements with a new line character. alert(result); } trimAllSpaces(); //Output //Thisisline1 //Thisisline2 //Thisisline3 </script>

Comments