String Methods
replace() – Method searches a string for a value or a regular expression. Method returns a new string with the value(s) replaced. Method does not change the original string.
replace once:
let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "W3Schools");
A global replacement:
let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/g, "red");
Example 1: /n to
var messagetoSend = document.getElementById('x').value.replace(/\n/g, "<br>");
or
str.replace(new RegExp('\r?\n','g'), '<br>');
or
messagetoSend = messagetoSend.replace(/\r\n/g, "<br>");