Skip to content

Instantly share code, notes, and snippets.

@remy
Forked from WebReflection/noswear.js
Last active September 10, 2018 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remy/26201b7c00887bd4ff01 to your computer and use it in GitHub Desktop.
Save remy/26201b7c00887bd4ff01 to your computer and use it in GitHub Desktop.
no swear
var terms = ['shit', 'fuck'].map(s => ' ' + s + ' ');
var re = RegExp(terms.join('|'));
var hasBadWords = function (str) { return re.test(' ' + str + ' '); };
hasBadWords("hit me duck"); // false
hasBadWords("holy shit"); // true
hasBadWords("My assistant couldn't find shitake mushrooms."); // false
@fearphage
Copy link

You don't need the map.

var re = new RegExp('\\b(?:' + words.join('|') + ')\\b');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment