Replace \n with <br/>

lulu50
lulu50 used Ask the Experts™
on
Hi,

i want to replace \n with </br>


 My original code is this: 

      var text = $("#ContidionsAndAttributesTextArea").val());
            alert(text); 
            editor.value = editor.value + text;


but I want to replace \n with <br/> 

            var text = $("#ContidionsAndAttributesTextArea").val().replace("\n", "<br />", "g");     //it's only replacing the first \n but not any \n found in the field
            alert(text); 
            editor.value = editor.value + text;

Open in new window


my output text is this:

"<br />IF Dx <> Dx2 (1237)  \n AND Px Range (22 - 333) "

It replace my first \n with <br> but not the second \n
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Daniel PineaultPresident / Owner CARDA Consultants Inc.
Distinguished Expert 2018

Commented:
What about
.replace(/\n/g, "<br />");

Open in new window

Or
.replace(new RegExp('\r?\n','g'), '<br />')

Open in new window

Or
.replace(/(?:\r\n|\r|\n)/g, '<br>');

Open in new window

Multitechnician
Top Expert 2014
Commented:
var text = $("#ContidionsAndAttributesTextArea").val().replace(/\n/g, "<br />");

test :
a.JPG
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
var text = $("#ContidionsAndAttributesTextArea").val().replace(/\n/g, "<br />");

Open in new window


Use the regex as above

Refer replace() method
lulu50Web application

Author

Commented:
leakim971

you are the best of the best !!!!

I thank you all for all your help.

fabuleux
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Just to point out, Daniel's answer above was actually the first correct one.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial