0

I'm trying to escape the backslash character in my regex.

Validators.pattern('^[^\\]*$')

but it return me this error:

ERROR SyntaxError: Invalid regular expression: /^[^\]*$/: Unterminated character class

I'm pretty sure my regex is good and I didn't see a solution in others similar topics.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3659739
  • 434
  • 6
  • 19
  • 1
    *I'm pretty sure my regex is good* - yes, the `^[^\\]*$` regex *string* will work in online regex testers but your *string literal* is actually representing ``^[^\]*$`` - try that in any online regex tester. You escaped a `]` and the `[` is no longer closed. Use `Validators.pattern(/^[^\\]*$/)` instead. – Wiktor Stribiżew May 14 '20 at 15:25
  • If it's necessary to work with a string, you need to use a quadruple backslash, resulting in a string containing a double-backslash. – Gershom Maes Jun 17 '20 at 12:45

0 Answers0