Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

discord.js Function to replace certain characters with a pattern

//This was created for my text based Discord game: Hangman
//This function will check the occurences of a certain letter in a word and return a formatted version
//Ex: word (not shown is mother), but is shown as - - - - - - (in bold with starting and ending **)
//User enters e
//This function then returns: - - - - e - (also in bold with **)

function display(text, chars = [], format = char => `**${char === null ? "-" : char}**`, separator = " ") {
    const items = [];
    text.trim().split("").forEach(char => {
        items.push(format(chars.indexOf(char) == -1 ? null : char));
    });
    return items.join(separator);
}
 
PREVIOUS NEXT
Tagged: #Function #replace #characters #pattern
ADD COMMENT
Topic
Name
7+9 =