Muhammad Umer
Muhammad Umer

Reputation: 18097

regex match range but not certain characters

How do i write regex so it matches A-Z but not characters b,h,i. Only way i could think of is by using custom ranges.

/[[A][C-G][J-Z]]/gi

This is what i can think of, i don't think this is correct regex, even. i'd like to not write custom ranges if possible. As it's complicate things a lot more.

What i'm trying to do is make characters increase by one so a becomes b, c -> d, z -> a. Except some words. And this is my strategy... find all words except them, run them through match string and replace them with character that is one ahead using charcode.

Upvotes: 2

Views: 3489

Answers (1)

anubhava
anubhava

Reputation: 784998

One way is to use negative lookahead:

/(?![BHI])[A-Z]/i

This will match anything in range [A-Z] except characters B, H and I.

RegEx Demo

Upvotes: 8

Related Questions