RegEx whitelist of words in any order
I came across the need to limit the words in a string to a certain list of words. This turns out to be a little more complicated than blacklisting a list of words, such as profanity, from a string.
Parameters
- Assume the whitelist is
foo bar baz
. - Match whitelisted words in any order
Pass:foo bar baz
Pass:bar foo baz
- Do not match on any word not in the whitelist
Fail:foo boom baz
- Do not match on extra inner, trailing or leading whitespace
Solution
Ruby
regex = %r{\A(?! )(?:(?:\A| )(?:foo|bar|baz)){1,3}\z}
JavaScript
const regex = /^(?! )(?:(?:^| )(?:foo|bar|baz)){1,3}$/
Shortcomings
The RegEx above does allow duplicate approved words up to the quantified limit. So 'foo bar bar'
will pass, but 'foo bar bar bar'
will not.
While I like to keep things succinct, this duplicate word problem can easily be avoided using other means.
Leave a Reply