Computer >> Computer tutorials >  >> Programming >> Javascript

How can I replace newlines with spaces in JavaScript?


To replace the newlines with spaces, use the JavaScript replace() method.

Example

You can try to run the following code to learn how to work with replace() method to replace newlines:

<!DOCTYPE html>
<html>
   <body>
   <p>Words:</p>
      <script>
         var words = "a\nb\nc\nd\ne\nf";
         words = words.replace(/\n/g, " ");
         document.write(words);
      </script>
   </body>
</html>