Regular expression in JavaScript to resolve a problem in a chat

0

I have the following problem, and that is that I have a chat to which I want to place a regular expression that substitutes a word for the nickname of the user who reads it. For example, I want to put a phrase that says

!! user are reading this

and I want the nickname of the person reading it instead of the user! Then I extracted the nickname of the user with which the corresponding command will be substituted. This is the code, it's with jQuery:

var usuario = $('#log_us').val();

the value that is obtained is the nick of the user, then I was experimenting:

function unescape2(m){
    m=unescape(m);
    m=m.replace(/</g,'&lt;');
    m=m.replace(/>/g,'&gt;');

m=m.replace(/!code(.*)/ig, function(m, gg) {
    if (gg == "" ) return m;
    else return "<div style=\"font-family:monospace; color:#fff; display:inline-block; padding:4px; background-color:#000;\">" + gg + "</div>";
});

m=m.replace(/!anc(.*)/ig, function(m, gg) {
    if (gg == "" ) return m;
    else return "<div class=\"anunciacion oficial\">" + gg + "</div>"
});

m=m.replace(/#meneo(.*)/ig,function(m,gg){
if(gg=='') return m;
return "<div style='display:inline-block;' class='shke'>"+gg+"</div>"
});

m=m.replace(/#uppi(.*)/ig,function(m,gg){
if(gg=='') return m;
return "<div style='-webkit-transform:rotate(-180deg); -moz-transform:rotate(-180deg); -o-transform:rotate(-180deg); transform:rotate(-180deg); display:inline-block;'>"+gg+"</div>"
});

    return m ;
}

that function is to enclose several commands, for example the #uppi makes the letters rotate 180 degrees, the #meneo move from side to side, and so, then I have no idea how to do what I want? Someone to guide me?

    
asked by Diesan Romero 17.07.2018 в 05:42
source

1 answer

1

Try adding:

m = 'Saludos !usuario.';
alert('Antes: ' + m);
m = m.replace(/!usuario\b/, $('#log_us').val());
alert('Después: ' + m);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="log_us" value="el_usuario" />
    
answered by 18.07.2018 в 20:47