Negate characters in Regular Expression [closed]

The caret inside of a character class [^ ] is the negation operator common to most regular expression implementations (Perl, .NET, Ruby, Javascript, etc). So I’d do it like this:

[^\W\s\d]
  • ^ – Matches anything NOT in the character class
  • \W – matches non-word characters (a word character would be defined as a-z, A-Z, 0-9, and underscore).
  • \s – matches whitespace (space, tab, carriage return, line feed)
  • \d – matches 0-9

Or you can take another approach by simply including only what you want:

[A-Za-z]

The main difference here is that the first one will include underscores. That, and it demonstrates a way of writing the expression in the same terms that you’re thinking. But if you reverse you’re thinking to include characters instead of excluding them, then that can sometimes result in an easier to read regular expression.

It’s not completely clear to me which special characters you don’t want. But I wrote out both solutions just in case one works better for you than the other.

Leave a Comment