javascript - Why regex for alphanumeric with hyphen and space is passing the special characters like , # etc -
this question has answer here:
- regex - space , special characters 4 answers
following regex
new regexp("[a-za-z0-9-]+$").test("@#2131")
which returns true. why allowing special characters : @ , #. dont understand reason , have cross checked many times major resources available on internet. what's wrong in expression?
the regex want write should follow rule : must start alpha numeric, can contain spaces , hyphens , must end alpha numeric characters. wrote
[0-9a-za-z]+[0-9a-za-z -]*[0-9a-za-z]+$
passed btw above mentioned text intest
method.
you forget add start of line anchor. without ^
, regex should check 1 or more alphanumeric chars + _
@ end of line.
new regexp("^[a-za-z0-9-]+$").test("@#2131")
or
/^[a-za-z0-9-]+$/.test("@#2131")
Comments
Post a Comment