1

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?

4

2 回答 2

3

空格在正则表达式中很重要。空格字符使您的{n,m}量词无效,导致{0, 15}*被评估为文字 string "{0, 15",后跟零个或多个}s。

此外,您可以浓缩[A-Za-z0-9_]\w

var filter = /^[ \w@./#&+-]{0,15}$/;
于 2013-07-04T09:20:18.447 回答
0

试试这个…………

^\w{1,15}$
于 2013-07-04T09:36:42.250 回答