I have a regular expression for a password field which I know works perfectly i.e. Must be at least 6 characters in length, have a capital letter, a number and can contain special characters. When I try to apply this regex within Javascript it doesn’t seem to validate. My Javascript function is below.
function (word) {
var weakRegEx = new RegExp('(?=^.{6,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$');
var result = weakRegEx.test(word);
return result;
Since you’re writing a string literal, you need to escape the
\characters.You should use a regex literal instead: