Javascript 字符串字母表前两个字符和后两个字符的正则表达式应为数字
我想验证一个字符串,比如Javascript 字符串字母表前两个字符和后两个字符的正则表达式应为数字,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我想验证一个字符串,比如 1.AB97CD11 案例 字符串的总长度最小为4,最大为8 前两个字符必须是字母 最后两个字符必须是数字 我尝试了这个正则表达式,但它对我不起作用: ^[a-zA-Z]{2}[a-zA-Z0-9]{4}[0-9]{2}$ 尝试以下模式: ^[A-Z]{2}[A-Z0-9]{0,4}[0-9]{2}$ 中间字符上的{0,4}宽度分隔符确保总长度必须介于4到8个字符之间。我假设你只需要大写字母。如果字母也可以是小写,那么使用[A-Za-z]而不是[A-z],因此我想
1.AB97CD11
案例
^[a-zA-Z]{2}[a-zA-Z0-9]{4}[0-9]{2}$
尝试以下模式:
^[A-Z]{2}[A-Z0-9]{0,4}[0-9]{2}$
中间字符上的
{0,4}
宽度分隔符确保总长度必须介于4到8个字符之间。我假设你只需要大写字母。如果字母也可以是小写,那么使用[A-Za-z]
而不是[A-z]
,因此我想您希望同时满足所有3个条件
您希望使用量词指定字母/数字的数量
[a-zA-Z]{2}[\w]{0,4}[0-9]{2}
我会做的
来自
是的,说真的。表现出一些努力,否则没有人会回答。等一下……我已经回答了。regex:“^[a-zA-Z]{2}[a-zA-Z0-9]{4}[0-9]{2}$”我尝试了这个,但没有使用
[a-zA-Z0-9]{0,4}
作为中间项,以允许总共4-8个字符。您当前的模式只允许8个字符。@Ajithkumar下次您提问时,请直接在问题中显示您的尝试您正在用PHP或JS运行正则表达式?您也可以在不使用正则表达式的情况下执行此操作。\w
还匹配下划线,OP没有提到的下划线在字符串中是允许的。没错,但他没有提到它是不允许的。也许你也可以用“.”作为任何字符的匹配
Match a single character present in the list below [a-zA-Z]{2}
{2} Quantifier — Matches exactly 2 times
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
Match a single character present in the list below [\w]{0,4}
{0,4} Quantifier — Matches between 0 and 4 times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equal to [a-zA-Z0-9_])
Match a single character present in the list below [0-9]{2}
{2} Quantifier — Matches exactly 2 times
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)