Format Text in TextAre

This is the initial text in Text Area

 After clicking on format button.
  1. <html>  
  2.   
  3. <head>  
  4.     <script type="text/javascript">  
  5.         function formattext(id)  
  6.         {  
  7.             var input = document.getElementById(id).value; // get textarea contents  
  8.             console.log("The First Time user enters data : " + input)  
  9.             input = input.replace(/ +/g, " "); //Removes Space from textarea contents  
  10.             console.log("When spaces are removed : " + input)  
  11.             input = input.replace(new RegExp('\r?\n''g'), '<br />'); //Removes new line  
  12.             console.log("When new line is removed : " + input);  
  13.             input = input.replace(/<br\s*\/?>/g, ''); //Removes br  
  14.             console.log("When br is removed : " + input)  
  15.             document.getElementById(id).value = input;  
  16.         }  
  17.     </script>  
  18. </head>  
  19.   
  20. <body>  
  21.     <textarea id="txtareaProjectDes" rows="4" cols="50">  
  22. </textarea>  
  23.     <button onclick="formattext('txtareaProjectDes')">Format Text</button>  
  24. </body>  
  25.   
  26. </html>