Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for allowing alphanumeric at the starting and hyphen thereafter

Tags:

regex

I had a requirement where in the text field the first character should be a alpha numeric and then i can allow a hyphen from thereafter in JavaScript.Also hyphen should not be allowed at the end

like image 649
GustyWind Avatar asked Jan 20 '09 07:01

GustyWind


1 Answers

If you do not want to match mutiple dashes after eachother:

^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$

This will match: a, a-a, aaa-a, aaa-a-aaa-a-aaa-a, etc
But not: -, -a, a-, a--a, a-a-a-, a-a--a, etc.

like image 104
wvanbergen Avatar answered Nov 07 '22 09:11

wvanbergen