Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read line break in a string with JavaScript [duplicate]

Tags:

javascript

Possible Duplicate:
Replace all line break in a string with <br /> tag in javascript

How can I read the line break from a value with JavaScript and replace it with a <br /> tag?

Example:

var a = "This is man.
     Man like dog."

I would like my result to look something like

var a = "This is man.<br />Man like dog.";
like image 262
Jin Yong Avatar asked Apr 24 '09 02:04

Jin Yong


2 Answers

var newString=oldString.replace(/\n/g,"<br />");
like image 81
Ali Avatar answered Sep 21 '22 22:09

Ali


To be complete: I've encountered cases where '\n' didn't work, in those cases I used:

someString.replace(/\n/g,'<br />')
          .replace(/\r/g,'<br />')
          .replace(/\r\n/g,'<br />');
like image 42
KooiInc Avatar answered Sep 18 '22 22:09

KooiInc