Quest 6: Check for profanity without using any profanity in your code (for family games)

const profanity = "~ç$|²\u0018­|¦+,|u©§|réí|·\u0006­|v'$|Á©ä|r\u0087$";
let swearJar = {
  counter:0,
  list:[]
};

function profanityChecker(data){
  let rgx = profanity;
  rgx = rgx.split("|").map(rgx => btoa(rgx)).join("|");
  rgx = new RegExp(rgx, "gi");
  if (rgx.exec(data)){
    //console.log("Profanity!")
    swearJar.counter++;
    swearJar.list.push(data)
    return true;
  }
  return false;
}

That is really cool. I dad not come across btoa before; what a bizarre name for a function.

I suggest modifying the second line of the function to this:

rgx = "\\b(?:" + rgx.split("|").map(rgx => btoa(rgx)).join("|") + ")";

As it is, you will catch Scunthorpe, a town in NE England, probably most famous for just this issue, which even has its own Wiki page:
https://en.wikipedia.org/wiki/Scunthorpe_problem

It checks the rude word is at the start of the a word, but not necessarily the end. You might want to adjust the profanity string to include MF therefore.


Ha! I was unaware of the Scunthorpeans.

I don't know. . .

If someone were to walk up to me and say, "Scunthorpe," out of the blue, I'd probably reply, "watch your mouth, damn it!"


It checks [if] the rude word is at the start of the [...] word, but not necessarily the end. You might want to adjust the profanity string to include MF therefore.

So, we either need a definitive list of profanity (impossible), or a list of innocent words which happen to include a profanity string (also impossible).

Or, we can just let 'em curse without awarding extra points for profanity.

(The game I actually use a swear jar in does in fact award the player extra points from the swear jar, but only if the player finished the story without getting the max score.)


The modified version might still reject Penistone, another English town.

As for offensive terms, you'd probabably need a list of all the likely compound words. Hyphenated terms (like some of your examples) should work; as \b matches a position between a word character (\w, being all alphabetic characters, modifying diacritics (accents), and the underscore) and either a nonword character (\W, anything else) or the beginning/end of the string.

If I remembered correctly.


This topic is now closed. Topics are closed after 14 days of inactivity.

Support

Forums