How to check if a string contains part of an array in Ruby?
2 answers
@nick =~ Regexp.new(bw.map { |w| Regexp.escape(w) }.join('|'))
Here we concatenate all strings into bw
a single regex, avoiding possible characters that have special meaning in the regex and validating the input against it.
Another way to achieve this functionality is to check every single word on the blacklist:
bw.any? { |w| @nick[Regexp.new(Regexp.escape(w))] }
Hope it helps.
+2
source to share