Remove Newline from Text of Element / Convert \n to <br />

I was trying to write an if statement to perform some actions when a certain error message was present. I went round and round trying to figure out why the text of the element wouldn't match in my if statement. Turns out it was because there was a line break before the text which is considered content. I stripped it out as you can see below:

var msg = "";
var applied = "You have already applied for this position.";
var msg = jQuery('.messages.status').text();
jQuery('.messages.status').text('');
 
msg = msg.replace(/\n/g,"");
 
jQuery('.messages.status').text(msg);			
 
	if(msg == "You have already applied for this position.")
	{
			// my actions here
	}

And if you want to replace a newline with a break tag:
string = string.replace(/\n/g,"<br />");