urbadave
urbadave

Reputation: 231

Regex 'blacklist' character validator returning odd result

Here's the Regex:

^[^~^\\/&$-+]*$

Here's the test string:

a(b

Even though ( doesn't appear in the blacklist, this returns NO matches. That makes NO sense. Anyone know why?

Upvotes: 0

Views: 379

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 31025

You have to put the dash at the end of the character class:

Change from:

^[^~^\/&$-+]*$
         ^---- Here the dash works as a range instead of a single character

To

^[^~^\/&$+-]*$
          ^--- Here works as the single "-" character

Btw, as Mr. Lama pointed in his comment you can also escape the dash like this:

^[^~^\/&$\-+]*$
         ^-- escaped here

Upvotes: 1

Related Questions