Để thay thế xuống dòng bằng khoảng trắng, dùng phương thức replace() của JavaScript.
Ví dụ
Chạy mã sau để học dùng replace() thay thế xuống dòng:
<!DOCTYPE html> <html> <body> <p>Từ:</p> <script> var words = "a\nb\nc\nd\ne\nf"; words = words.replace(/\n/g, " "); document.write(words); </script> </body> </html>