0

I have a password strength regex, apparently there is a different behavior using same regex steing, but different way to create it.

So i have one regex using slash slash closing / / And the other one is using new RegExp(string)

var password ="Password123";
var regex1 = new RegExp("((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^\da-zA-Z]).{6,})");
var regex2 = /((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^\da-zA-Z]).{6,})/;

var result1 = regex1.test(password);
var result2 = regex2.test(password);

document.getElementById("result1").innerHTML = result1;
document.getElementById("result2").innerHTML = result2;

https://jsfiddle.net/hansyulian/cnxzj262/

result1 is true, which is wrong, while result2 is false which is correct

Anyone have idea what happened? Is this a bug?

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
Hans Yulian
  • 1,080
  • 8
  • 26
  • The first one returns `true` because you told it to match a string that contains at least 1 `d` letter with `"(?=.*\d)"`. Double escape backslashes in constructor notation to define literal backslashes. – Wiktor Stribiżew Jul 19 '17 at 06:36
  • Oh myyy!!!! Yeaa i missed this concept.... thanx man... this explain the thing – Hans Yulian Jul 19 '17 at 06:40

0 Answers0