Let me outline the question more clearly, since it seems misunderstood in the comments.
To the core of it, what I want is to replace a specific character a with the input ab in the moment it is being typed. I cannot do a replace-all command, since that hits more a's than just the typed one.
The method I have shown below does remove the a and add ab instead, but not at the same location. The ab is added at the end of the input, not at the place where the a was typed. So it does not do replacement as wanted.
THE ACTUAL SITUATION:
When the enter button is pressed I want the resulting \n changed into <br>\n.
From much searching I have come to something like this (the enter key has keycode 13):
jQuery('textarea#comment').keyup(function(e){
    var content = jQuery(this).val();
    if(e.keyCode == '13') {
        e.preventDefault(); 
        content=content+"<br />\n";
        jQuery('textarea#comment').val(content);
    }
});
But this code piece does not replace the typed \n character with <br>\n. It merely prevents the typed character from being printed and then appends <br>\n at the end of the content. This will not work when the user moves his cursor and starts writing somewhere else than at the end. The <br>\n is simply being added at a wrong location.
The line content=content+"<br />\n"; is clearly incorrect to use. I need to know where the typed character is located so that I can replace it at that location or I need to edit the typed character on the fly before it is being printed on the screen.
Many other answers to similar questions give a replace-all solution. This will not work for me since the search term \n is included in the paste term <br>\n.
 
     
    
` to the end of the sentence, so why append `
\n` ? – Hamza Abdaoui Aug 11 '17 at 13:00
` tag to a `textarea` that obviously doesn't pass HTML. You need to use `\n` and not `
` if you're showing it in a text field. There's also no reason to reset the content of the textarea if you haven't actually added anything (which you haven't.. you only touch `content` if the keyCode is 13) – h2ooooooo Aug 11 '17 at 13:00
\n."* - do you? Are you sure this isn't an XY problem? What are you actually trying to solve? – freedomn-m Aug 11 '17 at 13:01
` added to have it saved in a database. I do though still want the line break `\n` to be shown during typing, so the text lines are still visually separated. – Steeven Aug 11 '17 at 13:10