Reputation: 18403
I want to negate part of a class in a regular expression. Let's say we have an expression accepting repeated big or small letters from a to z.
[a-zA-Z]+
How can I negate for example H
letter?
I've tried this [a-zA-Z^H]+
but it doesn't block H.
I know we can do it otherwise, but I search for a general rule to negate inside of a class.
I use JavaScript flavor of regex.
UPDATE
Here's the more specific example. Here's the expression: [\w\-\–]
. In .NET flavor it accepts unicode characters, but in JavaScript flavor it doesn't. There's a trick however to allow the expression to accept unicode chars too, \w
must be replaced with this expression ([^\x00-\x7F]|\w)
.
The problem is - it can't be nested inside first expression ([\w\-\–]
). That's why I'm asking how to make a negation of a part of a class.
Upvotes: 1
Views: 86
Reputation: 785471
Translating my comment into an answer. Here is a regex that you can use for negating certain character or range:
/((?:[^\x00-\x7F]|[-\w–])+)/
Upvotes: 1
Reputation: 46008
Normally you would use class subtraction
[a-zA-Z--[H]]
For engines that don't support character class subtraction (javascript) you can simply use a negative lookahead.
((?![H])[a-zA-Z])+
Working example: https://regex101.com/r/sE6tH0/6
http://www.rexegg.com/regex-class-operations.html#subtraction_workaround
Upvotes: 1