admin管理员组文章数量:1435235
I need a regex that will match a number containing six digits. The first digit must be 0, 1, or 2.
For example - 054654 or 198098 or 265876.
So far I got this: /^\d{6}$/
- matches a number containing six digits. How do I add the second requirement: first digit must be 0, 1, or 2?
I need a regex that will match a number containing six digits. The first digit must be 0, 1, or 2.
For example - 054654 or 198098 or 265876.
So far I got this: /^\d{6}$/
- matches a number containing six digits. How do I add the second requirement: first digit must be 0, 1, or 2?
- read a regex tutorial. – Casimir et Hippolyte Commented Jan 3, 2016 at 9:50
- Lazy me gets bad rep :) – Malasorte Commented Jan 3, 2016 at 9:52
4 Answers
Reset to default 3/^(0|1|2)\d{5}$/
That one should work
You can just use a character class:
/^[012]\d{5}$/
This tells the regex engine to match only one out of several characters, in this case 0
, 1
or 2
. To create a character class, place the characters you want to match between square brackets.
So the regex that you have written /^\d{6}$/ only matches 6 digits that start and end. You have to add (0|1|2) in the start where | means or and the final is /^(0|1|2)\d{5}$/ or /^[012]\d{5}$/.
This should do what you want:
/^(0|1|2)[0-9]{5}$/
|
means or
.
本文标签: javascriptRegexhas six digitsstarts with 01or 2Stack Overflow
版权声明:本文标题:javascript - Regex - has six digits, starts with 0, 1, or 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744689334a2619896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论