-3

Why do regex constructors need to be double escaped?

JohnPete22
  • 503
  • 3
  • 13

1 Answers1

1

The two regexes are different due to string interpolation. See this comparison from my Notepad++:

enter image description here

You need:

const regex = new RegExp(`^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!,%,&,@,#,$,^,*,?,_,~,+,\\-,",',.,:,=,{,},\\[,\\],(,)]).{${passwordMinLength},}`);
MonkeyZeus
  • 19,651
  • 3
  • 32
  • 72
  • Yes, based on the referenced article I came to that conclusion as well. But thank you for the text output, I had assumed they looked the same but I was mistaken. – JohnPete22 Feb 18 '21 at 18:33
  • @JohnPete22 A monospaced text editor only misleads under 2 conditions: incorrect output pasting and hidden chars like zero-width space chars and control characters. – MonkeyZeus Feb 18 '21 at 18:35