admin管理员组文章数量:1426451
So I'm pletely new to RegEx and I've read a few things and it's just blown my mind.
So far this is what I have
/^([a-z]{2})?([0-9])/i
What I basically have is a text box that needs to accept a string where the first 2 characters are letters and the rest are numbers, or just numbers.
Examples.
Match:
AB12345
12345
Not a match:
12345AB
AB12345AB
ACD1123
A332
Any help and information would be great so I can see how it works and hopefully understand it myself!
Thanks!
So I'm pletely new to RegEx and I've read a few things and it's just blown my mind.
So far this is what I have
/^([a-z]{2})?([0-9])/i
What I basically have is a text box that needs to accept a string where the first 2 characters are letters and the rest are numbers, or just numbers.
Examples.
Match:
AB12345
12345
Not a match:
12345AB
AB12345AB
ACD1123
A332
Any help and information would be great so I can see how it works and hopefully understand it myself!
Thanks!
Share Improve this question edited Aug 14, 2018 at 16:52 bobble bubble 18.7k4 gold badges31 silver badges50 bronze badges asked Aug 14, 2018 at 15:36 user1469914user1469914 511 silver badge10 bronze badges2 Answers
Reset to default 3You could take the start ^
and end $
of the string as well for checking, beside a quantifier for digits, one or more +
.
/^([a-z]{2})?\d+$/i
console.log(
['AB12345', '12345', '12345AB', 'AB12345AB', 'ACD1123', 'A332']
.map(s => /^([a-z]{2})?\d+$/i.test(s))
);
You miss end anchor($
) and digit repetition(\d+
):
const reg = /^([a-z]{2})?([0-9]+)$/i
console.log(['AB12345', '12345'].map(v => reg.test(v)))
console.log(['12345AB', 'AB12345AB', 'ACD1123', 'A332'].map(v => reg.test(v)))
本文标签: JavaScript RegExFirst 2 characters letterrest numbers OR just numbersStack Overflow
版权声明:本文标题:JavaScript RegEx - First 2 characters letter, rest numbers OR just numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745463270a2659420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论