1

I'm trying to know how many digits there is in a string that is essentially like a password.

For now I have this regex :

^(?=.*[0-9]{3,})([a-zA-Z0-9_/+*.-]{6,})$

It works great when their is 3 digits in a row but not when they are separated in the whole string.

I need to be able to know if there is 3 digit in strings like those :

h123dasd 1hkh/23jd 1gvbn/*2fefse-

What can I do ?

rXp
  • 617
  • 2
  • 12
  • 25

2 Answers2

3

You can use this regex:

/^(?=(?:\D*\d){3,})[a-zA-Z0-9_/+*.-]{6,}$/

This will enforce 3 digits in your input that may or may not be consecutive.

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

No need for such a complicated regex IMO - just extract digits from the string, concat the matches, and then check the length. Something like:

str.match(/\d+/g).reduce((p, c) => p + c).length > 3;

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95