JavaScript RegExp [^abc] Character Class

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is [^abc]?

[^abc] is a negated character class. It matches exactly one character that is not a, b, or c. The ^ must be the first character inside the brackets to mean “not.”

Remember
/[^abc]/     → one char that is NOT a, b, or c
/[abc]/      → one of a, b, or c (the opposite)
/[^abc]+/    → one or more chars outside {a,b,c}

"cab".match(/[^abc]/g)  → null
"cat".match(/[^abc]/g)  → ["t"]

^ inside []  → negate the class
^ outside [] → start-of-string anchor
UsePrefer
Match anything except a/b/c/[^abc]/
Match a, b, or c/[abc]/
Remove a, b, and cstr.replace(/[abc]/g, "")

How to Use [^abc]

Write the class in a regex, then use test, match, or replace.

  1. Write /[^abc]/ (add g to find every match).
  2. Use test() to check if any non-a/b/c character exists.
  3. Use match() to collect characters outside the set.
  4. List more letters after ^ when you need a bigger exclude set.
PieceRequired?Role
[^abc]YesOne character that is not a, b, or c
Quantifier (+, *, …)OptionalMatch a run of allowed (non-excluded) characters
Flag gOptionalFind / replace all matches
Flag iOptionalAlso treat A, B, C as excluded

Pseudocode

Pseudocode
pattern = [^abc]     // "not a, not b, not c"

for each character ch in text:
    if ch is NOT in {a, b, c}:
        ch matches
    else:
        ch does not match

// with + : match the longest run of such characters

Cheat Sheet

Keep these facts handy while writing custom exclude sets.

TopicDetail
Syntax/[^abc]/
MeaningOne character that is not a, b, or c
Opposite[abc]
One or more/[^abc]+/
Case-insensitive exclude/[^abc]/i
Bigger set/[^abcxyz]/ or /[^abc0-9]/
StandardCore regex (ES3+) — universal support

Live Preview

Pick a sample string and see every match of /[^abc]/g.

Browser demo of /[^abc]/g — same idea as the examples below.

Result
text = "cat"
text.match(/[^abc]/g)
→ ["t"]

/* a and c skipped; t is outside {a,b,c} */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Detect Non-a/b/c with test()

Check whether a string contains any character outside {a, b, c}.

JavaScript
const notAbc = /[^abc]/;

console.log(notAbc.test("cab"));   // false
console.log(notAbc.test("cat"));   // true
console.log(notAbc.test("Code"));  // true
Try It Yourself

How It Works

  1. Build the pattern. /[^abc]/ means “one character not a, b, or c.”
  2. Only a/b/c. "cab" has no match → false.
  3. Extra letter. "t" in "cat" matches → true.
  4. Uppercase. C, o, d, e are outside the lowercase set → true.

Example 2 — Collect Matches with match()

Use the global flag to list every character that is not a, b, or c.

JavaScript
const text = "a1b!";

console.log(text.match(/[^abc]/g));   // ["1", "!"]
console.log(text.match(/[^abc]+/g));  // ["1", "!"]
console.log("cab".match(/[^abc]/g)); // null
Try It Yourself

How It Works

  1. Global scan. /g finds every separate match.
  2. Skip a and b. Digits and symbols stay; a and b are skipped.
  3. With +. Here matches are isolated, so + gives the same list.
  4. No matches. "cab" is only a/b/c → null.

Example 3 — Keep Only a, b, and c

Strip everything outside the set — the inverse of matching [^abc].

JavaScript
const text = "backpack";

console.log(text.replace(/[^abc]/g, ""));  // "baac"
console.log(text.replace(/[abc]/g, ""));   // "kpk"
console.log(/[^abc]/.source);               // "[^abc]"
Try It Yourself

How It Works

  1. Keep the set. Replace every [^abc] match with "" → only a/b/c remain.
  2. Remove the set. replace(/[abc]/g, "") does the opposite — deletes a/b/c.
  3. Source string. .source shows the pattern text inside the slashes.
  4. Common use. Filter input down to an allowed alphabet.

Edge Cases & Pitfalls

Check these before relying on [^abc] in real projects.

trap

^ inside vs outside brackets

Inside [] at the start, ^ negates. Outside, ^ is the start anchor. /^[abc]/ means “a/b/c at start,” not “not a/b/c.”

case

Case-sensitive by default

A, B, and C match /[^abc]/ because only lowercase letters are excluded. Use /[^abc]/i to exclude both cases.

one

Matches one character by default

Add + or * when you need a whole run of non-excluded characters, not a single character.

null

match returns null when nothing fits

Strings made only of a, b, and c yield null. Guard with || [] if you always want an array.

Browser & Runtime Support

Negated character classes like [^abc] are core JavaScript regular-expression syntax (ES3+) and work everywhere.

✓ Baseline · ES3+

RegExp [^abc]

Supported in all evergreen browsers, Internet Explorer, and every current Node.js / Deno / Bun release.

100% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 4+ · Legacy environments
Full support
Opera All modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
[^abc] Universal

Bottom line: Safe everywhere. Use [^abc] to exclude a custom set; use [abc] to match only that set.

Performance Notes

ApproachCostBest for
/[^abc]/.test(s)O(n)Quick “has non-a/b/c?” check
s.match(/[^abc]/g)O(n)List every excluded-set miss
s.replace(/[^abc]/g, "")O(n)Keep only a, b, and c

Character-class matching is linear in string length for these patterns. Prefer test when you only need a yes/no answer.

Key Takeaways

  • Role: [^abc] matches one character that is not a, b, or c.
  • Opposite: [abc] matches only those three letters.
  • Filter: replace(/[^abc]/g, "") keeps only a, b, and c.
  • Caret: ^ inside [] negates; outside it anchors the start.

One line: [^abc] means “not a, b, or c” — the inverse of [abc] for a custom character set.

Frequently Asked Questions

[^abc] is a negated character class. The caret ^ right after [ inverts the set, so the pattern matches any single character that is NOT a, b, or c.
[abc] matches a, b, or c. [^abc] matches everything else—digits, other letters, spaces, and punctuation. They are opposites for those three characters.
No. Every character in "cab" is a, b, or c, so [^abc] finds no match. test() returns false and match() returns null.
Yes, by default. Uppercase A, B, and C are not in the excluded set, so they match [^abc]. Use the i flag if you want to exclude both cases: /[^abc]/i.
Yes. List them after ^: [^abcxyz] excludes six letters. You can mix ranges too: [^abc0-9] excludes a, b, c, and all digits.
No. Outside brackets, ^ means start of string (or line with m). Inside brackets at the start of the class, ^ means “not.”

Next: (x|y)

Learn how alternation lets a pattern match one branch or another.

(x|y) tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful