So, I was working on some regex and came across some weird regex behavior.
I had a regex character class that included a bunch of characters (alphanumeric) and ended with a space, a dash, and a plus. Strange behavior is reproduced using the following regular expression.
^[ -+]*$
So what happens is that space is a valid text input, and that is a plus. However, for some reason, a dash is not a valid text input. The regular expression can be fixed by rearranging the characters in the class as follows:
^[ +-]*$
Now all characters are valid. This was reproduced in Chrome using jsFiddle as well as using Expresso.
My question is basically, am I doing something wrong or is it just weird? :)
source
share