1function nl2br(str){
2 return str.replace(/(?:\r\n|\r|\n)/g, '<br>');
3}
4var stringWithNewLines= `Well this is a
5a very broken
6sentance`;
7var stringWithBrs=nl2br(stringWithNewLines);
8// stringWithBrs= "Well this is a<br>a very <br>broken<br>sentance"
1let multilineString = `My
2text\r1\r\n2\n3\n\r4
3is
4here`;
5
6let htmlText = multilineString.replace(/(\r\n|\n\r|\r|\n)/g, '<br>');
7
8console.log('Multiline string: ' + multilineString);
9console.log('HTML text: ' + htmlText);