我有一个正则表达式,用于验证电话号码字符串为空或包含10-14位任何格式的数字。它至少需要10个字符,但仍可以匹配14个数字。我之前很少使用前瞻性功能,也没有看到问题。这是注释中预期的解释:
/// ^ - Beginning of string
/// (?= - Look ahead from current position
/// (?:\D*\d){10,14} - Match 0 or more non-digits followed by a digit, 10-14 times
/// \D*$ - Ending with 0 or more non-digits
/// .* - Allow any string
/// $ - End of string
^(?=(?:\D*\d){10,14}\D*|\s*$).*$
这在System.ComponentModel.DataAnnotations.RegularExpressionAttribute
中用于asp.net MVC 5站点,因此它与.NET Regexes一起在服务器端使用,而在带有jquery validate的javascript中与客户端一起使用。如果字符串包含的位数超过14位,如何停止匹配?
正则表达式的问题