index.html
1 2 3 4 5 6 7 8 9 10 11 |
Enter text with line breaks and convert breaks to "\n", so all text is in a single line. <div> <h2>Input (text with linebreaks)</h2> <textarea id="input"></textarea> <br><button id="process" onClick="onProcess()">Process</button> </div> <div> <h2>Output</h2> <textarea id="output"></textarea> </div> |
style.css
1 2 3 4 5 |
#input, #output { width: 100%; height: 100px; } |
script.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function onProcess() { var input = document.getElementById("input").value; if (!input) { // Null or undefined or bad input alert("Invalid input"); } var output = ""; // Replace line-breaks with "\n" output = input.replace(/(?:\r\n|\r|\n)/g, '\\n'); document.getElementById("output").value = output; } |