简体   繁体   中英

How to check if a string contains special characters (characters other than a-z 0-9 and _) with regex

I have a form where users write their username, and I want to check if their username has special characters in it. (characters other than az 0-9 and _)

How can I do that with regex?

You can use character class ( \\W ) which means anything except alphabets, digits and _

 const strArr = [`Dragon!"£123`, `Clown %^☻#52_`, `Fish_12354`] let check = (str) => console.log(str, /\\W/.test(str)) strArr.forEach(check)

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