I have this regex which checks if the password has one digit at least.
^(?=.*[0-9]).{6,}$
How do I modify the regex to check if the sum of all the digits in the password is equal to say 10.
So this string should match "dhbqdw46". This shouldn’t "jwhf1ejhjh0".
2
Answers
Regular expressions are designed for pattern matching and cannot perform arithmetic operations like calculating the sum of digits.
As mentioned in the comments, you’ll need to implement the logic yourself. Since you’ve tagged this question with JavaScript, here’s a solution using JavaScript:
RegEx can’t do that and is certainly not meant to do that. To check this kind of stuff you need to write your own JavaScript.
If you want you can use regex to extract all digits from the string and then sum them up using JavaScript (See Variant 1) or you just use JavaScript and do not use regex at all (See Variant 2).
Variant 1
Variant 2