JSFiddle - React, Tailwind, and code Playground
by neonDog
JavaScript
const regex = /[\p{L}-]+/ug;
const isBlackListed = (str = '', listed = [], fuzzy=false) => (
listed.reduce((matches, word) => {
const index = str.indexOf(word);
if (index > 0) {
if (fuzzy) {
matches.push({ word, index });
} else {
const prevChar = str[index-1] || ' ';
const nextChar = str[index+word.length] || ' ';
//console.log(prevChar, nextChar);
console.error(prevChar, regex.test(prevChar));
if (!(regex.test(prevChar) || regex.test(nextChar))) {
console.log(`This bio matched %c${word}`,
'background-color: teal; font-weight:bold; padding: 3px 8px;',
`with a word length of ${word.length} at index ${index}.`);
matches.push({ word, index });
}
}
}
return matches;
}, [] // <-- matches array
)
);
console.log(isBlackListed('my dog can amoss poo can chuckypoo', ['can', 'am', 'chucky', 'poo'], true));