Javascript REGEX does not end with special character but returns true

console.log(/^[0-9a-zA-Z]+[~!@#$%^&*_+-=]+$/.test("123456"));
      

Run codeHide result


I think it should return false

because the string doesn't end with a character ~!@#$%^&*_+-=

but it does return true

when it starts. Why is he returning true

?

+3


source to share


1 answer


The problem is that +-=

is a range. If you look at the table of the ASCII , you will see that +-=

include +

, -

, .

, /

, :

, ;

, <

, =

and all the numbers 0

.. 9

.



You want to [~!@#$%^&*_+\-=]

( get out of -

).

+5


source







All Articles