Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to convert newlines to breaks

I'm trying to convert newlines to breaks. But for some reason I can't get this to work. I did various tests, and I know the function is being executed, and that the variable passed to the function is definitely a string. It's weird that this does not work. Since I use exactly the same function for double escaping newlines before I send it to the server. I have tried a lot of things, and do not understand why this does not work.

console.log(ev);
var parsedJSON = JSON.parse(ev.data);
console.log('string that goes in function:' + parsedJSON.message);
//typeof(parsedJSON.message); string
var bericht = placeBreaks(parsedJSON.message);
    function placeBreaks(str)  {
        return str.replace(/\r?\n/g, "<br />");
    }
    console.log('string being returned: ' + bericht);

json: { "date": 1431199838, "name": "Root", "message": "test\ntest"}

(index):167 string that goes in function:test\ntest

(index):173 placeBreaks return: test\ntest

like image 691
DDaems Avatar asked Nov 09 '22 13:11

DDaems


1 Answers

you can use

replace \n with \r

try this in vim editor

%s/\\n/\r/gec 
like image 132
Zam Avatar answered Nov 14 '22 21:11

Zam