Reputation: 45692
I have the next regex: ^[a-z\d\-,:\s]+$
, now I need it to disallow whitespaces at the beggining of the string, so I do - /^[a-z\d][a-z\d\-,:\s]?[a-z\d]$/i
.
Problem: The input may be 0,1,2+ characters, but this regex require at least 2
Question: how to do the same and allow zero or one character?
Upvotes: 1
Views: 74
Reputation: 785156
You can use lookarounds to disallow space at start or end:
/^(?!\s|.*\s$)[a-z\d,:\s-]*$/
(?!\s|.*\s$)
is a negative lookahead to assert that position at start and end is not a space.
Upvotes: 1