Space in regex character class creates weird results

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? :)

+4
source share
2 answers

A character -has special meaning within character classes. When it appears between two characters, it creates a range, for example. [0-9]matches any character between 0and 9, inclusive. However, when placed at the beginning or at the end of a character class (or when escaped), it is a literal character -.

  • [ -+]will match any character between space (char code 32) and +(char code 43) inclusive.
  • [ +-] (char 32), + (char 43) - (char 45)
+6

- "" , a-z

, space to +, . -, \ , ,.

+3

Source: https://habr.com/ru/post/1544775/


All Articles