[英]Regex to validate alphanumeric string with 1-5 digits in JavaScript
I need to validate an alphanumeric string where it contains at least 1 digit and at most 5 digits, ie accept characters [a-zA-Z0-9]
with the above restriction.我需要验证一个字母数字字符串,其中它包含至少 1 位数字和最多 5 位数字,即接受具有上述限制的字符
[a-zA-Z0-9]
。
I am working on JavaScript.我正在研究 JavaScript。
You can use /^[a-zA-Z]*([0-9][a-zA-Z]*){1,5}$/
:您可以使用
/^[a-zA-Z]*([0-9][a-zA-Z]*){1,5}$/
:
var samples = ['asldi', 's23', '2341123', '221', 'sdf3'] samples.forEach(s => { console.log(s + ": ", /^[a-zA-Z]*([0-9][a-zA-Z]*){1,5}$/.test(s)) } )
with:和:
^[a-zA-Z]*
matches possible leading letters; ^[a-zA-Z]*
匹配可能的前导字母;([0-9][a-zA-Z]*){1,5}
matches 1 to 5 digits with possible intermediate letters; ([0-9][a-zA-Z]*){1,5}
匹配 1 到 5 个可能有中间字母的数字;$
matches the end of string; $
匹配字符串的结尾;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.