I'm trying to write a script that deals with passwords. I want to find the first match on a regular expression in a long string. For example, if I have the string testingtestingPassWord12#$testingtesting
and I want to find the first instance that has 2 uppercase, 2 lowercase, 2 numbers, 2 special characters and a minimum length of 12, it should return PassWord12#$
. If I want the same criteria but with a length of 16 it should return tingPassWord12#$
.
This is what I have for a regular expression: (?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{12}
That regex is based on this SO: Regex to validate password strength
I tried the following but it just returns the first 12 characters in the string instead of the matched string:
var str = 'testingtestingPassWord12#$testingtesting',
re = /(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{12}/;
console.log(str.match(re));
// Output: ["testingtestingPa"]
What am I missing?