RegEx for including alphanumeric and special characters
In your character class the )-'
is interpreted as a range in the same way as e.g. a-z
, it therefore refers to any character with a decimal ASCII code from 41 )
to 96 '
.
Since _
has code 95, it is within the range and therefore allowed, as are <
, =
, >
etc.
To avoid this you can either escape the -
, i.e. \-
, or put the -
at either the start or end of the character class:
/^[a-zA-Z0-9!@#$&()`.+,/"-]*$/
There is no need to escape the "
, and note that because you are using the *
quantifier, an empty string will also pass the test.
Using this regex you allow all alphanumeric and special characters. Here \w
is allowing all digits and \s
allowing space
[><?@+'`~^%&\*\[\]\{\}.!#|\\\"$';,:;=/\(\),\-\w\s+]*
The allowed special characters are ! @ # $ & ( ) - ‘ . / + , “ = { } [ ] ? / \ |
You need to escape the hyphen:
"^[a-zA-Z0-9!@#$&()\\-`.+,/\"]*$"
If you don't escape it then it means a range of characters, like a-z
.