简体   繁体   中英

Regular Expressions (regex): /W with hyphen (javascript)

I want to validate an user input. The user is allowed to write all characters, digits and "_" or "-".

I'm using

myString.search(/\W/) != -1

to validate. It works for everything I want, but not for the "-". How can I search for "no wordcharacter" but the hyphen "-" is allowed?

解决方案应该是myString.search(/[^\\w-]/) != -1

This should work:

myString.search(/[\W-]/) != -1

It's basically matching against a character class that includes any "no word character" and a hyphen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM