replace all occurrences in a string [duplicate]
Use the global flag.
str.replace(/\n/g, '<br />');
Brighams answer uses literal regexp
.
Solution with a Regex object.
var regex = new RegExp('\n', 'g');
text = text.replace(regex, '<br />');
TRY IT HERE : JSFiddle Working Example