Reputation: 7752
I have a regex like this:
var filter = /^[ A-Za-z0-9_@./#&+-]*$/;
I want this regex to return true if there is no character on input field or if there is character less than 14. I tried using this:
var filter = /^[ A-Za-z0-9_@./#&+-]{0, 15}*$/;
But this regex is never returning true. It always return false even after I satisfy the condition. What's wrong?
Upvotes: 1
Views: 536
Reputation: 336308
Whitespace is significant in a regular expression. The space character makes your {n,m}
quantifier invalid, causing {0, 15}*
to be evaluated as the literal string "{0, 15"
, followed by zero or more }
s.
Also, you can condense [A-Za-z0-9_]
into \w
:
var filter = /^[ \w@./#&+-]{0,15}$/;
Upvotes: 3