Reputation: 2354
The following rules apply to the string I need to test:
^[a-z]+[\w]*[a-z0-9]$/gi
is the closest I got but it doesn't match a string that contains a single letter.
Examples that should match:
Examples that should not match:
^[a-z]+[\w]*[a-z0-9]*$/gi
also allows the string to end with _
Upvotes: 1
Views: 1053
Reputation: 92854
Another way with regex alternation group:
^([a-z]+[\w]*[a-z0-9]|[a-z]+)$
Upvotes: 1
Reputation: 626728
You may use an optional group:
/^[a-z]+(?:\w*[a-z0-9])?$/i
Details
^
- start of string[a-z]+
- one or more letters(?:\w*[a-z0-9])?
- one or zero occurrences of
\w*
- 0 or more word chars[a-z0-9]
- one alphanumeric char$
- end of string.Upvotes: 2