Skip to content
Advertisement

How to remove char from regex

I’m not got, yet, with regex. I’ve been trying to break my head to get this to work.

I need a regex that allows the user to enter

  • Any alphabetical char (a-z)
  • Any number
  • For special char only “-” and “_”.
  • “@” is not allowed.

I got this but no dice. [^a-zA-Z0-9]

Thanks

Advertisement

Answer

^[w-]+$

will match a string following the rules you describe. w matches letters, digits, or underscore, then it adds - to that set. Anchoring with ^ and $ requires all the characters in the string to match this pattern.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement