0

I am making a filter for a chat room I own.
I was succesful in having it turn NSFW words into a bunch of symbols and astericks to censor it, but many people bypass it by simply putting a backslash, period, or other symbol/letter after it because I only put in the words without the punctation and symbols. They also come up with a bit more creative methods such as eeeNSFWeee so the filter doesn't count it as a word.
Is there a way to make it so that the filter will select certain characters that form a word in a string and replace them (with or without replacing the extra characters connected to the message)?

The Filter is made in javascript and Socket.io

Filter code:

    const array = [
  "NSFW",
  "Bad Word"
  "Innapropiate Word"
];

message = message
  .split(" ")
  .map((word) => (array.includes(word.toLowerCase()) ? "$#!%" : word))
  .join(" ");

For an example if somebody typed "Bad Word" exactly like that (caps are not a problem), it would censor it succesfully.
But if somebody typed "Bad Word." that would be a problem because since it has a period it would count it as a different word, thats what I need fixed.

ChocolateChara
  • 187
  • 1
  • 16

1 Answers1

0

There are a number of approaches you could take here.

You could use replace() if you just want to remove symbols. For example:

word.replace(/[&\/\\#,+()$~%.`'"!;\^:*?<>{}_\[\]]/g, '')

You could use Regular Expressions in general, which allows you to match on patterns instead of exact string matching.

You could also use more complex fuzzy matching libraries or custom fuzzy matching to accomplish your goal. This post may be helpful.

kg1313
  • 66
  • 5
  • Can you use a variable attached to an array to specify a substring using the string.prototype.replace method in the Regular Expressions? – ChocolateChara Jan 24 '22 at 21:57
  • You could, but you could also use an "or" operator in regex instead. For example: `const badWords = /NSFW|Bad Word|Innapropiate Word/gi const censoredMessage = message.replace(badWords, '$#!%')` – kg1313 Jan 24 '22 at 22:39
  • I messed around with it a bunch and it works! Thankyou so much – ChocolateChara Jan 26 '22 at 04:12