How to match only some characters with a regex?
regex, ruby
Solution
You need to make sure that your string consists only of those characters by anchoring the regex to the beginning and end of the string:
/^[abc]+$/
You also mixed up two concepts. Alternation (which would be `(a|b|c)`) and character classes (which would be `[abc]`). They are in this case equivalent. Your version would also allow `|` as a character.
Problem
I want to check if a string matches only some characters with a regex. For example, I would like to match only `a`, `b`, or `c`. So, "aaacb" would pass, but "aaauccb" would not (because of the `u`). I have tried this way: ``` /[a|b|c]+/ ``` but it does not work, because the failing example passes.