[Solved] Highlight ping if the user is in the chatroom, Javascript


What you can do, is keep a list of usernames on the clients and server (give to new clients connecting), and then check if that username is in that list, if so then highlight the ping.

The code I got was:

function highlight(message){
    if(message == "") {
        return message
    }
    let mentions = message.match(/@\b([A-Za-z0-9]+)\b/g)
    if (mentions === null) { return message }
    for (i = 0; i < mentions.length; i++) {
        let mention = mentions[i].substring(1)
        if(sesskx.has(mention)) {
            message = message.replace(mentions[i], `<span class="name-color">@${mention}</span>`)
        }
    }
    return message
 };

Explaination:

  1. It takes the message, and finds all mentions
  2. it loops over all mentions
  3. if the mention is in current users it highlights it

EDIT: repl link: https://repl.it/@Codemonkey51/Froggy-Chatroom

12

solved Highlight ping if the user is in the chatroom, Javascript