简体   繁体   中英

JavaScript Regex for first character alphabet only, second and onwards can be alphanumeric or special characters (hyphen, comma and whitespace)

I need help in regex where first character alphabet only, second and onwards can be alphanumeric or special characters (hyphen, comma and whitespace).

I tried below its is not working.

^[a-zA-Z][a-zA-Z0-9 -,]+$

You were almost there, simply escape - in the second character set, like so:

^[a-zA-Z][a-zA-Z0-9 \-,]+$

Otherwise, as pointed out by @Sweeper in the comments, -, would match any character between and , .

See explanation .

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