-1

I need a regex expression for pattern validation that should allow only alphanumeric words and some special characters that are !@#$%^&*()-_

I tried that expression but it didn't work

Validators.pattern("^[a-zA-Z0-9!@#$%^*-_&()\\\"]*$")
Gurmanjot Singh
  • 9,774
  • 2
  • 19
  • 39
Raza Ellahi
  • 349
  • 2
  • 6
  • 19

1 Answers1

1

Your problem is the *-_ inside your character set. This is treated as a range of all the characters from * to _ i.e. *, +, , ... ], ^, _ (I've left out the rest of the characters for brevity). You need to either put the - at the beginning or end of the set, or escape it within the set e.g.

Validators.pattern("^[a-zA-Z0-9!@#$%^*_&()\\\"-]*$")
Nick
  • 123,192
  • 20
  • 49
  • 81