Files
habitica/website/server/libs/stringUtils.js
borisabramovich86 026014b8d6 lists banned words in the chat error message - fixes https://github.com/HabitRPG/habitica/issues/8812 (#8858)
* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* add test to check the error message contains the banned words used

* improve banned words test

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* add test to check the error message contains the banned words used

* improve banned words test

* merge with develop - aligned banned slurs check with banned words check
2017-08-02 12:43:22 -07:00

19 lines
631 B
JavaScript

export function removePunctuationFromString (str) {
return str.replace(/[.,\/#!@$%\^&;:{}=\-_`~()]/g, ' ');
}
export function getMatchesByWordArray (str, wordsToMatch) {
let matchedWords = [];
let wordRegexs = wordsToMatch.map((word) => new RegExp(`\\b([^a-z]+)?${word.toLowerCase()}([^a-z]+)?\\b`));
for (let i = 0; i < wordRegexs.length; i += 1) {
let regEx = wordRegexs[i];
let match = str.toLowerCase().match(regEx);
if (match !== null && match[0] !== null) {
let trimmedMatch = removePunctuationFromString(match[0]).trim();
matchedWords.push(trimmedMatch);
}
}
return matchedWords;
}