How To Replace \n With
In JavaScript?
I have a textarea where I insert \n when user presses enter. Code from this textarea is sent to a WCF service via jQuery.ajax(). I cannot save \n in DB as it won't show in other ap
Solution 1:
Replace with global scope
$('#input').val().replace(/\n/g, "<br />")
or
$('#input').val().replace("\n", "<br />", "g")
Solution 2:
it could be done like this:
$('textarea').val().replace(/\n/g, "<br />");
edit: sorry ... the regular expressions in javascript should not be quoted
Solution 3:
Like said in comments and other answer, it's better to do it on server side.
However if you want to know how to do it on clientside this is one easy fix:
textareaContent.replace(/\\n/g, "<br />");
Where textareaContent
is the variable with the data in the textarea.
Edit: Changed so that it replaces globally and not only first match.
Solution 4:
If you support PHP you should check this out: http://php.net/manual/en/function.nl2br.php
Solution 5:
You can use a simple javascript string function.
string.replace("\n", "<br>")
Post a Comment for "How To Replace \n With
In JavaScript?"