-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
  • 10,224
  • 2
  • 19
  • 43
Raza Ellahi
  • 379
  • 2
  • 7
  • 20

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
  • 138,499
  • 22
  • 57
  • 95