0

I am new to Javascript regex and trying to convert a Java based regex pattern into Javascript. But, I am facing a problem in escaping special characters in the character class. I have added backslashes but still, the problem exists.

Code:

const regexp2 = /^(?=.*[*.!@$%^&(){}[]:;<>,.?/~_+-=|\]).{6,12}$/;
John Doe
  • 137
  • 4
  • `[]` should be `[\]` – depperm Aug 12 '21 at 12:04
  • `]` is special inside character classes. Also, `-` must be escaped or put at the start/end of the character class. So, use `/^(?=.*[*.!@$%^&(){}[\]:;<>,.?\/~_+=|-]).{6,12}$/`. `/` inside square brackets can usually be left as is, unescaped, but it is a good idea to escape it (especially if you need to test it online). – Wiktor Stribiżew Aug 12 '21 at 12:04
  • / is also a special character. Escape it like this: `\/`. – RatajS Aug 12 '21 at 12:05

0 Answers0