iinux
iinux

Reputation: 43

Javascript REGEX not end with some special character but return true

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

I think it should return false because the string does not end with a ~!@#$%^&*_+-= character, but it returns true when it runs. Why does it return true?

Upvotes: 3

Views: 49

Answers (1)

melpomene
melpomene

Reputation: 85767

The problem is that +-= is a range. If you look at the ASCII table, you can see that +-= includes +, -, ., /, :, ;, <, =, and all digits 0 .. 9.

You want [~!@#$%^&*_+\-=] (escape the -).

Upvotes: 6

Related Questions