i would like to replace the space and line break in a textarea, if the input is
________________________________________
i am a
fat
boy zzz
________________________________________
the result should be
i<sp>am<sp>a<br>fat<br><br>boy<sp>zzz
where space is replaced by <sp>
and newline is replaced with <br>
.
The space replacement is successfully done,
but i failed when i try to replace new line with <br>
(i tried 3-4 different methods but none of them can make it)
it would be grateful if anyone have idea? thanks in advance
The code is as follows
<!DOCTYPE html>
<html>
<body>
<textarea id="txtArea" rows="10" cols="70">i am fat boy </textarea>
<input type="text" id="StringTextBox" value="" >
<p id="demo"> </p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("txtArea").value;
var res = str.split(' ').join('<'+"sp"+'>');
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
in JavaScript?](http://stackoverflow.com/questions/6331895/how-to-replace-n-with-br-in-javascript) – Rick S Mar 16 '15 at 15:45
")