现有正则排除ComponentA,如何修改以额外排除ComponentB?
Got it, let's break this down and fix your regex.
First, let's recap how your original regex works:/^((?!.*?ComponentA).)*$/ uses a negative lookahead ((?!...)) to ensure that the entire string never contains "ComponentA" anywhere in it. The .*? makes it check for the substring regardless of where it appears, and the wrapping ((?!...).)* ensures every character in the string passes this check.
To add exclusion for "ComponentB", you just need to extend that negative lookahead to check for either substring. There are two clean ways to do this:
Option 1: Combine Exclusions with a Group (Concise)
This version bundles both excluded components into a single lookahead, making the regex shorter:
{ regex: /^((?!.*?(ComponentA|ComponentB)).)*$/ }
Option 2: Separate Lookaheads (More Explicit)
If you prefer clarity over brevity, you can chain two separate negative lookaheads. This does the exact same thing but makes it obvious you're excluding two distinct items:
{ regex: /^(?!.*?ComponentA)(?!.*?ComponentB).*$/ }
Pro tip: This second option is slightly more efficient because it checks both conditions once at the start of the string, instead of re-checking for every character like the first version.
How to Verify It Works
- ✅ Matches: Any string that doesn't include "ComponentA" or "ComponentB" (e.g.,
HomePage.js,Utils.ts) - ❌ Doesn't Match: Strings with either substring (e.g.,
ComponentA.vue,FeatureComponentB.js,ComponentAHelper.ts)
内容的提问来源于stack exchange,提问作者Radex

