After considering your request for "at least" three digits, the solution doesn't have to be overly complex. Here is a simple regex that should suffice:
/\d.*\d.*\d/
This pattern matches a digit followed by zero or more characters, then another digit, and so on. Since I haven't anchored either end, it allows for any number of characters before and after the digits.
console.log(!!"abk2d5k6".match(/\d.*\d.*\d/)); // true
console.log(!!"abk25k6d".match(/\d.*\d.*\d/)); // true
console.log(!!"abkd5k6".match(/\d.*\d.*\d/)); // false (A digit was removed)
console.log(!!"abk2k6d".match(/\d.*\d.*\d/)); // false (A digit was removed)
Alternatively, you can use:
/(\d.*){3}/
console.log(!!"abk2d5k6".match(/(\d.*){3}/)); // true
console.log(!!"abk25k6d".match(/(\d.*){3}/)); // true
console.log(!!"abkd5k6".match(/(\d.*){3}/)); // false (A digit was removed)
console.log(!!"abk2k6d".match(/(\d.*){3}/)); // false (A digit was removed)
As suggested by m.buettner in the comments, instead of using .
, you can utilize \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 (A digit was removed)
display(!!"abk2k6d".match(/\d\D*\d\D*\d/)); // false (A digit was removed)
display(!!"abk2d5k6".match(/(\d\D*){3}/)); // true
display(!!"abk25k6d".match(/(\d\D*){3}/)); // true
display(!!"abkd5k6".match(/(\d\D*){3}/)); // false (A digit was removed)
display(!!"abk2k6d".match(/(\d\D*){3}/)); // false (A digit was removed)
In summary, there's no need for anchors when seeking an "at least" match.