Reputation: 412
I want a regular expression to test a string that meets the following requirements:
a-z
A-Z
and 0-9
-
and _
but may not start nor end with these charactersIt should match the following strings:
Could someone please help me with it? Thanks in advance!
Upvotes: 0
Views: 1048
Reputation: 324800
You have your pieces, so put them together:
/^[a-z0-9](?:[a-z0-9_ -]*[a-z0-9])?$/i
This does exactly what you're looking for. The only complicated part is allowing a single character.
Upvotes: 3