简体   繁体   English

正则表达式匹配范围,但不是某些字符

[英]regex match range but not certain characters

How do i write regex so it matches AZ but not characters b,h,i. 我该如何写正则表达式,使其匹配AZ但不匹配字符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. 我想做的是使字符增加一,因此a变为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. 这是我的策略...查找除单词以外的所有单词,将它们贯穿在匹配字符串中,并使用charcode将其替换为前面一个字符。

One way is to use negative lookahead: 一种方法是使用负前瞻:

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

This will match anything in range [AZ] except characters B , H and I . 这将匹配[AZ]范围内的所有字符,但字符BHI除外。

RegEx Demo 正则演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM