1

I have this password regex:

var PasswordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z!@#$&()\\-`.+,/\"]{8,}$/;

i now want the min length to be set from the admin CMS

so i have this

function CheckPassword(minLengh, Password) {
DynamicPasswordRegex = new RegExp("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z!@#$&()\\-`.+,/\"]{" + minLengh + ",}$");

return DynamicPasswordRegex.test(Password);
}

for some reason, passwords that are ok, returned false

Cœur
  • 37,241
  • 25
  • 195
  • 267
Y.G.J
  • 1,098
  • 5
  • 19
  • 44

1 Answers1

2

You're writing a string literal, so your backslashes are being parsed as string literal escape sequences.

You need to escape every backslash as \\ so that the regex sees a single backslash.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964