VB_
VB_

Reputation: 45692

regex alphanum without spaces at the end/beginning of the string

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

Answers (1)

anubhava
anubhava

Reputation: 785156

You can use lookarounds to disallow space at start or end:

/^(?!\s|.*\s$)[a-z\d,:\s-]*$/

RegEx Demo

(?!\s|.*\s$) is a negative lookahead to assert that position at start and end is not a space.

Upvotes: 1

Related Questions