How can I replace newlines with spaces in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 09-Jan-2018 09:13:39
Advertisements