[英]Need regular expression to allow maximum of 3 special character
我需要一个满足以下要求的正则表达式。
尝试过此操作,但未按预期工作。
^[a-z\s]{0,50}[.\-']*[a-z\s]{0,50}[.\-']*$
如果有人做对了,请告诉我。
好吧,您可以编写一些难以理解或无法维护的正则表达式,也可以仅编写说明规则的代码:
function validate(str) {
var not_too_long = str.length <= 50,
has_no_dots = !/\./.test(str),
not_too_many_specials = (str.match(/[^\w\s]/g) || []).length <= 3;
return not_too_long && has_no_dots && not_too_many_specials;
}
根据您对“特殊字符”的定义进行适当的调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.