What is the correct way to strip profane words from a string given:
1) I have a list of 100 words to look for in an array of strings.
2) What is the correct way to handl
(foobar|foobaz|...)
Then put guards on either side of the grouping for extraneous characters
[^!@#$%^&*]*(foobar|foobaz|foofii)[^!@#$%^&*]*
Also, you'll probably want to use a case insensitive flag so that it'll also match words like FooBaz and fOObaR.
As far as performance goes, concatenating this as one big regex is probably fastest (although I'm not an expert). The regex algorithm is pretty efficient at searching & handling branch conditions. Basically, it must be better than O(mn)
(where m
is the number of words and n
is the size of the text you're searching)