The replace() method of String returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. (info from MDN)

Here, please note that

The replace is done for a single occurrence at most unless global flag is set.

And the global flag (g) can only be set with a regular expression.

For example,

var msgStr = 'Error 1.\nError 2.\nError 3.\n';

// The following won't work as it replaces only one instance of '\n'.
var msgHtml = msgStr.replace('\n', '<br>');

// This works as expected.
var msgHtml = msgStr.replace(/\n/g, '<br>');