javascript - Why regex for alphanumeric with hyphen and space is passing the special characters like , # etc -


this question has answer here:

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 in test 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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -