Balram Singh
Balram Singh

Reputation: 1744

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 -,]+$

Upvotes: 1

Views: 602

Answers (1)

Lyubomir
Lyubomir

Reputation: 20037

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.

Upvotes: 2

Related Questions