Words that are in a variable have the form 1 2 3 need to find these words in another variable and replace them with other words (you can replace everything with the same word)

So far, I use
filter.mat.some(fw => { if (message.content.includes(fw)) { // Код } }

(it is necessary to make a variable replacing the words in the first variable with any text or other words)

    1 answer 1

    If I understood correctly, it can be done something like this -

     let sourceText = `abcdeabcde`; let templateText = `ace`; const CONFIGURATION_MAP = { 'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', }; const createMap = (text, map) => text.split(' ').reduce( (result, key) => { let value = typeof map === 'object' ? map[key] : map; return value ? Object.assign(result, {[key]: value}) : result; }, {} ); const replace = (template, map) => Object.keys(map).reduce( (result, mark) => { let regexp = new RegExp( `${mark}`, 'g' ); let value = map[mark]; return result.replace( regexp, value ); }, template ); let map = createMap(templateText, CONFIGURATION_MAP); let word = createMap(templateText, '*'); let resultWithMap = replace(sourceText, map); let resultWithSingleWord = replace(sourceText, word); console.log(`With map: ${resultWithMap}`); console.log(`With single word: ${resultWithSingleWord}`); 

    • Everything fits, but you can replace everything with one (For example, b with A, d with A and e with A? (Inconveniently all words in the CONFIGURATION_MAP will be entered)) - sasha0552
    • @ sasha0552 made changes. - user220409