admin管理员组文章数量:1287583
Is there any regex (!) which can test if a string contains 3 digits ( never-mind the order) ?
(at least 3 digits. also I be happy to see the exact 3 solution.( if you're kind ))
example :
abk2d5k6 //3 digits
abk25k6d //same here //3 digits
my fail tries :
.+?(?=\d){3}
Thanks.
(only regex solutions please , for learning purpose.).
Is there any regex (!) which can test if a string contains 3 digits ( never-mind the order) ?
(at least 3 digits. also I be happy to see the exact 3 solution.( if you're kind ))
example :
abk2d5k6 //3 digits
abk25k6d //same here //3 digits
my fail tries :
.+?(?=\d){3}
Thanks.
(only regex solutions please , for learning purpose.).
Share Improve this question edited Nov 25, 2012 at 9:28 Royi Namir asked Nov 25, 2012 at 9:14 Royi NamirRoyi Namir 149k144 gold badges492 silver badges829 bronze badges 4- 2 There is. What have you tried? – John Dvorak Commented Nov 25, 2012 at 9:16
- 1 Also, at most three, at least three or exactly three? – John Dvorak Commented Nov 25, 2012 at 9:16
-
@JanDvorak Ive tried a lot. (believe me). I tried with positive look ahead but got stuck with
\d+
( theplus
thing. - cause the plus wants it to be sequential). for your second question , lets say at least 3. ( sorry for not clarify it). – Royi Namir Commented Nov 25, 2012 at 9:19 - @RoyiNamir: If you've tried things, show what you've tried, why you thought that was the way to go, and the results you were getting that weren't what you expected. – T.J. Crowder Commented Nov 25, 2012 at 9:24
3 Answers
Reset to default 11Well, for the learning purpose, I suggest to read through this very prehensive tutorial.
Otherwise, the JavaScript regex would probably look something like this:
if you want to make sure that there are at least three digits:
/^(?:\D*\d){3}/
Or if you want to make sure that there are exactly three digits:
/^(?:\D*\d){3}\D*$/
\D
is any non-digit character. *
allows 0 or more of those. \d
is any digit character. {3}
repeats the subpattern 3 times. And ^
and $
mark the start and end of the string, respectively.
You could also do something like this:
str.match(/\d/g).length >= 3
This is dead simple, and very clearly shows the intent without a plicated regex.
This isn't tuned for speed though.
Given your ment that you want "at least" three digits, I don't think it needs to be any more plicated than:
/\d.*\d.*\d/
...which is a digit, then zero or more of anything, then another digit, then zero or more of anything, then another digit. As I haven't anchored either end, there's an implicit "zero or more of anything" at both ends.
console.log(!!"abk2d5k6".match(/\d.*\d.*\d/)); // true
console.log(!!"abk25k6d".match(/\d.*\d.*\d/)); // true
console.log(!!"abkd5k6".match(/\d.*\d.*\d/)); // false (I removed a digit)
console.log(!!"abk2k6d".match(/\d.*\d.*\d/)); // false (I removed a digit)
or
/(\d.*){3}/
console.log(!!"abk2d5k6".match(/(\d.*){3}/)); // true
console.log(!!"abk25k6d".match(/(\d.*){3}/)); // true
console.log(!!"abkd5k6".match(/(\d.*){3}/)); // false (I removed a digit)
console.log(!!"abk2k6d".match(/(\d.*){3}/)); // false (I removed a digit)
or at m.buettner points out in the ments below, rather than .
you can use \D
(not a digit):
/\d\D*\d\D*\d/
or
/(\d\D*){3}/
display(!!"abk2d5k6".match(/\d\D*\d\D*\d/)); // true
display(!!"abk25k6d".match(/\d\D*\d\D*\d/)); // true
display(!!"abkd5k6".match(/\d\D*\d\D*\d/)); // false (I removed a digit)
display(!!"abk2k6d".match(/\d\D*\d\D*\d/)); // false (I removed a digit)
display(!!"abk2d5k6".match(/(\d\D*){3}/)); // true
display(!!"abk25k6d".match(/(\d\D*){3}/)); // true
display(!!"abkd5k6".match(/(\d\D*){3}/)); // false (I removed a digit)
display(!!"abk2k6d".match(/(\d\D*){3}/)); // false (I removed a digit)
Basically, no need for anchors if it's an "at least" match.
本文标签: javascriptRegex for 3 digits (nevermind the order)Stack Overflow
版权声明:本文标题:javascript - Regex for 3 digits (never-mind the order)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741245322a2364763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论