JavaScript RegExp [^abc] Character Class

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Excluded letters

What You’ll Learn

The negated class [^abc] matches any single character except lowercase a, b, or c. It extends the negation idea from [^0-9] to a custom letter set—ideal for filtering, validation, and cleanup when certain characters must be excluded.

01

Negate

[^abc]

02

Exclude

Not a, b, c

03

Opposite

of [abc]

04

Filter

replace → ""

05

Expand

[^abc0-9]

06

^ first

After [ only

Introduction

You already know that [abc] matches one letter from a small set. The negated form [^abc] inverts that rule: match anything except those three letters.

This is useful when a few characters are forbidden—blocking specific letters in usernames, stripping unwanted symbols from tokens, or validating that input does not contain particular characters.

Understanding the [^abc] Character Class

The caret ^ immediately after [ negates everything that follows inside the brackets until ]. For [^abc], any character other than lowercase a, b, or c is a match.

JavaScript
/[^abc]/.test("dog");  // true  ("d" is allowed)
/[^abc]/.test("cab");  // false (only a, b, c present)
/[^abc]/.test("a1z");  // true  ("1" and "z" match)

Digits, uppercase letters, spaces, and punctuation all match [^abc] because they are outside the excluded trio.

💡
Beginner Tip

To keep only a, b, and c, remove everything else: str.replace(/[^abc]/g, ""). That is the mirror image of using [^abc] to find what you want to delete.

How to Use [^abc] in JavaScript

Common workflows mirror other character classes—detect, extract, replace, and validate:

JavaScript
const hasOther = /[^abc]/.test("cab");       // false
const hasOther2 = /[^abc]/.test("cat");      // true ("t")

const onlyABC = "a1b!c2x".replace(/[^abc]/g, ""); // "abc"
const outsiders = "a1b2c".match(/[^abc]/g);       // ["1", "2"]

Use test() to check for forbidden characters, match() to list them, and replace(/[^abc]/g, "") to keep a sanitized subset.

📝 Syntax

JavaScript
/[^abc]/              // one char not in {a,b,c}
/[^abc]+/             // one or more excluded chars in a row
/[^abc0-9]/           // not a,b,c and not a digit
str.replace(/[^abc]/g, "")  // keep only a, b, c

Common patterns

  • /[^abc]/ — find any character outside the set.
  • /^[^abc]+$/ — entire string contains no a, b, or c.
  • /[abc]/ — positive counterpart (matches a, b, or c).
  • !/[^abc]/.test(s) — confirm string uses only a, b, c.

⚡ Quick Reference

GoalPattern
One char not a/b/c[^abc]
Keep only a, b, cstr.replace(/[^abc]/g, "")
No a, b, c anywhere/^[^abc]*$/ or !/[abc]/.test(s)
List forbidden chars foundstr.match(/[^abc]/g)
Exclude letters + digits[^abc0-9]
Positive set[abc]

📋 [^abc] vs [abc] vs [^0-9]

Three negated and positive classes with different exclusion rules.

Allow abc
[abc]

3 letters

Block abc
[^abc]

Everything else

Block digits
[^0-9]

Non-digits

Combined
[^abc0-9]

Letters+digits out

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover detection, extraction, sanitizing, validation, and combined exclusions.

📚 Getting Started

Detect characters outside the a–b–c set.

Example 1 — Test for Characters Outside a, b, c

Return true when the string contains any letter or symbol not in the excluded set.

JavaScript
const notABC = /[^abc]/;

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

How It Works

"cab" uses only allowed letters, so every character fails the negated class and test() returns false. "cat" contains t, which matches [^abc].

📈 Practical Patterns

Extract, sanitize, and validate with negated sets.

Example 2 — List Every Character Not in a, b, c

Find digits and other letters mixed into a token.

JavaScript
const token = "a1b2c";

const outsiders = token.match(/[^abc]/g);
console.log(outsiders); // ["1", "2"]

const first = token.match(/[^abc]/);
console.log(first[0]); // "1"
Try It Yourself

How It Works

Digits are not a, b, or c, so they match [^abc]. The global flag collects every outsider in one array.

Example 3 — Sanitize Input to Keep Only a, b, and c

Remove digits and symbols by deleting everything matched by [^abc].

JavaScript
const messy = "a1b!c2x-z";

const clean = messy.replace(/[^abc]/g, "");
console.log(clean); // "abc"

const alsoClean = messy.replace(/[^abc]/gi, ""); // case-insensitive if needed
Try It Yourself

How It Works

Replacing each [^abc] match with an empty string strips digits, punctuation, and other letters, leaving only the allowed trio.

Example 4 — Validate a Token With No a, b, or c

Reject strings that contain any of the forbidden letters.

JavaScript
function hasForbiddenLetters(text) {
  return /[abc]/.test(text);
}

console.log(hasForbiddenLetters("dog"));  // false (no a,b,c)
console.log(hasForbiddenLetters("back")); // true  (contains a,b,c)

const noABC = /^[^abc]+$/;
console.log(noABC.test("xyz"));  // true
console.log(noABC.test("xay"));  // false
Try It Yourself

How It Works

Checking for forbidden letters is often clearer with the positive class /[abc]/. The anchored negated form /^[^abc]+$/ requires every character to fall outside the banned set.

Example 5 — Exclude Letters and Digits with [^abc0-9]

Match only punctuation and symbols by negating both a custom letter set and digits.

JavaScript
const raw = "a1!b2@c3#";

const symbols = raw.match(/[^abc0-9]/g);
console.log(symbols); // ["!", "@", "#"]

const lettersAndDigits = raw.replace(/[^abc0-9]/g, "");
console.log(lettersAndDigits); // "a1b2c3"
Try It Yourself

How It Works

You can chain exclusions inside one class. [^abc0-9] negates both the listed letters and the digit range, leaving symbols as the matches.

🚀 Common Use Cases

  • Input sanitization — keep only approved letters in a custom alphabet.
  • Forbidden character checks — block specific letters in codes or labels.
  • Token cleanup — strip digits and punctuation from mixed strings.
  • Data validation — ensure slugs exclude reserved letters.
  • Highlighting — wrap characters outside an allowed set for review UI.
  • Building larger exclusions — combine with ranges like [^abc0-9].

Important Considerations

  • Case sensitivity[^abc] excludes lowercase only; uppercase A, B, C still match unless you add them: [^abcABC].
  • ^ placement — negation works only when ^ is the first character after [.
  • One vs many — a single [^abc] matches one character; use + for consecutive runs.
  • Positive check alternative — “only a,b,c allowed” can be written as !/[^abc]/.test(s) on non-empty strings or validated per character.
  • Hyphen in class — to exclude a literal hyphen along with letters, place - at the start or end: [^-abc].

🧠 How [^abc] Matches Text

1

Invert the set

^ after [ removes a, b, and c from the allowed characters.

Negate
2

Read character

The engine tests the next input character against the inverted set.

Compare
3

Act on match

Use in test, match, or replace depending on whether the outsider should be detected or removed.

Apply
4

Allow or reject

Outside abc → match. Inside abc → skip.

📝 Notes

  • [^abc] is the complement of [abc] for single-character positions.
  • Extend exclusions: [^abcxyz], [^a-z], [^abc0-9].
  • Review [abc] for the positive set and [^0-9] for digit exclusion.
  • Next up: (x|y) alternation for matching one pattern or another.
  • For Unicode letter classes, consider modern \p{} properties when targeting international text.
  • Always use the g flag when removing all excluded characters with replace.

Browser & Runtime Support

Negated custom character classes like [^abc] are part of core JavaScript regular expression syntax.

Baseline · ES3+

RegExp [^abc]

Supported in every browser and Node.js version. Combined exclusions such as [^abc0-9] use the same universal syntax.

99% Universal syntax
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · 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] Excellent

Bottom line: Safe everywhere. Negated character classes behave consistently across all JavaScript runtimes.

Conclusion

The [^abc] negated class completes the character-class toolkit alongside [abc], [0-9], and [^0-9]. It matches anything except the listed letters, making it perfect for filtering, validation, and cleanup.

Place ^ first inside brackets, combine exclusions when needed, and pair with replace to keep sanitized alphabets. Next, learn (x|y) alternation to match entire pattern alternatives.

💡 Best Practices

✅ Do

  • Put ^ immediately after [ to negate
  • Use replace(/[^abc]/g, "") to keep allowed letters
  • Add uppercase to the exclusion when case matters
  • Combine ranges for compact exclusions: [^abc0-9]
  • Prefer /[abc]/ when checking for forbidden letters

❌ Don’t

  • Confuse [^abc] with literal sequence (abc)
  • Assume uppercase is excluded without listing it
  • Forget g when stripping all outsiders
  • Place ^ away from the start unless you mean a literal caret
  • Overuse negated classes when a positive check reads clearer

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp [^abc]

Exclude specific letters from matches in JavaScript.

5
Core concepts
🔄 02

vs [abc]

Opposite.

Compare
🛠 03

Sanitize

replace g.

Pattern
04

Combine

[^abc0-9].

Expand
05

Case

Add ABC.

Pitfall

❓ 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 in that string. test() returns false and match() returns null.
Yes. List them after ^: [^abcxyz] excludes six letters. You can mix ranges too: [^abc0-9] excludes a, b, c, and all digits.
Immediately after the opening bracket: [^abc]. If ^ appears elsewhere inside the class, it is usually a literal caret character.
Negated character classes are core regex syntax since ES3. [^abc] works in every browser and Node.js version.
Did you know?

Negated and positive classes are complements: anything that matches [^abc] does not match [abc] at the same position, and vice versa. Together they partition the set of possible single characters (for a given case and Unicode rules).

Continue to (x|y) alternation

Learn how to match one pattern or another using parentheses and the pipe operator.

(x|y) tutorial →

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.

6 people found this page helpful