当4个字母字符连续出现时,Regex进行过滤

regex to filter whenever 4 alphabetical characters are present consecutively

本文关键字:Regex 过滤 4个 字符 连续      更新时间:2023-09-26

不允许在一行中写入4个字母字符。如:

ABCD: error
ABC1: allowed
ABC1233...: allowed

只要键入第四个字母字符,就会出现错误。
我试过了:

(/^[a-zA-Z]{3,}.+$/.test(value))

在输入abcd:时,它给出错误,这是正常工作的
再次显示错误。这个错误不正确。

如果我理解你的话,你想有3个字母,然后其他的都是字母?

找到regex:

^[a-zA-Z]{3}[^a-zA-Z]+$

如果你只想要字母后面的数字,那就这样做:

^[a-zA-Z]{3}[0-9]+$

仅搜索[a-zA-Z]{4,}的字符串,如果有任何匹配,则在字符串的一行中有四个字母数字字母,如果没有匹配,则允许字符串

EDIT
看来你的要求如下:
您希望接受仅包含字母数字字符的字符串,不包含开头为4个或以上连续字母的字符串,并且不包含为空。

在这种情况下,你的正则表达式应该是这样的:

           /^(''d|[a-zA-Z]{1,3}(''d|$))/
            v '_/ '___________/'_____/
            |  |        |          |
         ___|  |        |          |
 _______|_    _|___    _|_____    _|____________
|match the|  |match|  |match  |  |match either  |
|beginning|  |one  |  |1 to 3 |  |a digit or the|
|of string|  |digit|  |letters|  |end of srtring|
 ---------    -----    -------    --------------

可以解释为:

Match the beginning of the string  
  followed by:  
    either: one digit
    or    : one to three letters
              followed by:
                either: one digit
                or    : the end of the string
示例代码:
var strArr = ["ABC", "ABCD", "ABC123DEFG", "ABCD1234"];
var regex = new RegExp("^(''d|[a-zA-Z]{1,3}(''d|$))");
for (var i = 0; i < strArr.length; i++) {
    alert(strArr[i] + ": " + (regex.test(strArr[i]) ? "ALLOWED" : " NOT ALLOWED"));
}


看到,这个更新(* *)短演示