JavaScript RegExp [abc] Character Class

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Custom character set

What You’ll Learn

The [abc] character class matches exactly one character from a list you choose—in this case a, b, or c. It is the building block for custom letter sets, category codes, and flexible pattern matching beyond simple ranges like [0-9].

01

Class

[abc]

02

One of

a, b, or c

03

Order

Doesn’t matter

04

Quantifiers

+, {n}

05

Case

Sensitive

06

Not (abc)

Class vs group

Introduction

Character classes let you match one character from a defined set. While [0-9] covers all digits with a range, [abc] lists specific characters explicitly. The engine picks whichever single letter appears at the current position.

Use this pattern when only a handful of letters are valid—quiz answers, hex digits without full ranges, product codes, or filtering text that must use a limited alphabet.

What Is the [abc] Character Class?

Square brackets create a character class. Each character listed inside is an alternative: the pattern matches if the next input character equals any one of them.

JavaScript
/[abc]/.test("dog");  // false (no a, b, or c)
/[abc]/.test("cab");  // true  (matches "c")
/[abc]/.test("A");    // false (uppercase A is different)

[abc], [bac], and [cab] are equivalent—order inside brackets does not change meaning. This is different from parentheses (abc), which match the literal three-letter sequence in order.

💡
Beginner Tip

Think of [abc] as “pick one letter from this menu.” To match a whole word built only from those letters, add + and anchors: /^[abc]+$/.

How to Use [abc] in JavaScript

Combine the class with RegExp methods the same way you would [0-9] or any other pattern:

JavaScript
const hasLetter = /[abc]/.test("hello");       // true  (contains "a")
const firstHit  = "xyz".match(/[abc]/);          // null
const allHits   = "alphabet".match(/[abc]/g);    // ["a", "b", "a"]
const cleaned   = "a1b!c".replace(/[^abc]/g, ""); // "abc"

Use test() for presence checks, match() to collect hits, and replace() to keep or remove characters based on membership in your set.

📝 Syntax

JavaScript
/[abc]/               // one of a, b, c
/[abc]+/              // one or more from the set
/^[abc]$/             // exactly one letter: a, b, or c
new RegExp("[abc]")   // constructor form

Common quantifiers with [abc]

  • [abc]? — zero or one character from the set.
  • [abc]+ — one or more characters, each from a, b, or c.
  • [abc]{3} — exactly three characters from the set (e.g. "bac").
  • ^[abc]+$ — entire string uses only a, b, and c.

⚡ Quick Reference

GoalPattern
Any one of a, b, c[abc]
Same meaning, reordered[cab] = [abc]
Sequence of allowed letters[abc]+
Single-letter code only/^[abc]$/
Include uppercase too[abcABC] or /[abc]/i
Literal sequence “abc”(abc) not [abc]

📋 [abc] vs [a-z] vs (abc)

Three similar-looking patterns with very different meanings.

Custom set
[abc]

3 letters only

Range
[a-z]

All lowercase

Group
(abc)

Literal "abc"

Negated
[^abc]

Not a, b, or c

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover testing, matching, validation, and filtering with a custom letter set.

📚 Getting Started

Check whether text contains a, b, or c.

Example 1 — Test for Membership with test()

See which strings contain at least one character from the set.

JavaScript
const letterSet = /[abc]/;

console.log(letterSet.test("dog"));     // false
console.log(letterSet.test("cab"));     // true
console.log(letterSet.test("hello"));   // true  (contains "a" and "b")
Try It Yourself

How It Works

test() returns true as soon as it finds any character equal to a, b, or c anywhere in the string.

📈 Practical Patterns

Extract, validate, and filter using the custom set.

Example 2 — Find All Matching Letters with match()

Collect every a, b, or c in a longer word.

JavaScript
const word = "alphabet";

const first = word.match(/[abc]/);
console.log(first[0]);           // "a"

const all = word.match(/[abc]/g);
console.log(all);                // ["a", "b", "a"]
Try It Yourself

How It Works

Each [abc] consumes one matching character. The g flag repeats the search to find every hit, including repeated letters.

Example 3 — Validate a Single-Letter Code

Accept only one character that must be exactly a, b, or c.

JavaScript
const codePattern = /^[abc]$/;

function isValidCode(code) {
  return codePattern.test(code);
}

console.log(isValidCode("b"));   // true
console.log(isValidCode("d"));   // false
console.log(isValidCode("ab"));  // false (two characters)
Try It Yourself

How It Works

Anchors ^ and $ require the entire string to be a single character from the allowed set—useful for multiple-choice or rating codes.

Example 4 — Validate a Word Using Only a, b, and c

Ensure every character in the input belongs to the set, with one or more letters allowed.

JavaScript
const wordPattern = /^[abc]+$/;

console.log(wordPattern.test("bac"));   // true
console.log(wordPattern.test("ccab"));  // true
console.log(wordPattern.test("back"));  // false ("k" not allowed)
console.log(wordPattern.test("ABC"));   // false (uppercase)
Try It Yourself

How It Works

[abc]+ repeats the class so each position must be a, b, or c. The plus requires at least one character; combine with anchors to validate the full token.

Example 5 — Keep Only a, b, and c with replace()

Strip digits and symbols, leaving only characters from your allowed alphabet.

JavaScript
const messy = "a1b!c2x";

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

const highlighted = messy.replace(/[abc]/g, "[$&]");
console.log(highlighted); // "[a]1[b]![c]2x"
Try It Yourself

How It Works

[^abc] matches anything not in the set. Removing those characters leaves a clean string. Wrapping matches with [$&] highlights allowed letters in place.

🚀 Common Use Cases

  • Multiple-choice validation — accept only a, b, or c answers.
  • Custom alphabets — restrict input to a small allowed character set.
  • Text filtering — extract or highlight specific letters from mixed content.
  • Game logic — validate moves or tokens drawn from a limited letter pool.
  • Parsing codes — match status flags represented by single letters.
  • Building larger classes — combine sets: [abc0-9] for letters plus digits.

Important Considerations

  • One character per token — bare [abc] matches a single position; use + for sequences.
  • Case sensitivity[abc] excludes uppercase unless you add ABC or the i flag.
  • Not a literal word[abc] matches "cab" one letter at a time; it is not the same as matching the word "abc" in order unless quantifiers align.
  • Special characters — inside [ ], most characters are literal; place - at the start/end to match a hyphen literally.
  • Metacharacters], \, and ^ (at the start) need careful placement or escaping inside classes.

🧠 How [abc] Matches Text

1

Define the set

Characters inside [ ] form an allowed list. Here: a, b, c.

Pattern
2

Read next char

The engine checks whether the current input character is in the set.

Compare
3

Repeat if +

With a quantifier, the engine keeps matching allowed letters consecutively.

Quantify
4

Match or fail

In the set → success. Not in the set → no match at this position.

📝 Notes

  • Combine with digits: [abc0-9] matches one letter a–c or one digit.
  • Negate the set: [^abc] matches any character except a, b, and c.
  • For all lowercase letters, prefer [a-z] over listing 26 characters.
  • Use explicit sets when the allowed pool is small and non-contiguous.
  • See [0-9] for digit ranges and [^0-9] for negated classes.
  • Inside classes, . is usually a literal dot, not “any character.”

Browser & Runtime Support

The [abc] character class uses core JavaScript regular expression syntax supported everywhere.

Baseline · ES3+

RegExp [abc]

Supported in every browser and Node.js version. No polyfill required.

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. Custom character classes work identically across all JavaScript runtimes.

Conclusion

The [abc] character class is a simple, powerful way to match one character from a hand-picked set. It extends the same bracket syntax you learned with [0-9] to arbitrary letters and symbols.

Use it for validation, filtering, and extraction when only specific characters are allowed. Remember the difference from (abc), add quantifiers for longer tokens, and normalize case when uppercase matters.

💡 Best Practices

✅ Do

  • Use [abc] for small, explicit letter sets
  • Add + and anchors for full-string validation
  • List uppercase letters when case matters
  • Combine sets: [abc0-9] for mixed alphabets
  • Prefer [a-z] when the whole alphabet applies

❌ Don’t

  • Confuse [abc] with capturing group (abc)
  • Assume [abc] matches the word “abc” as a unit
  • Forget case sensitivity for user input
  • Put an unescaped ^ first unless you mean negation
  • Over-list characters when a range would be clearer

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp [abc]

Custom character sets in JavaScript regex.

5
Core concepts
🔢 02

One of

a, b, or c.

Meaning
🔄 03

Order free

[cab] OK.

Rule
04

+ repeat

Long tokens.

Pattern
05

Not (abc)

Class vs group.

Pitfall

❓ Frequently Asked Questions

[abc] is a character class that matches exactly one character—either a, b, or c. The order inside the brackets does not matter: [abc] is the same as [cab] or [bac].
Not by itself. Each [abc] token matches one character. The string "abc" matches when you use [abc] three times or [abc]+ to match one or more letters from the set in a row.
Yes. [abc] matches lowercase a, b, and c only. To include uppercase, list them explicitly: [abcABC] or use the i flag with a pattern that accounts for both cases.
[abc] is a character class matching one letter from the set. (abc) is a capturing group matching the literal sequence "abc" in that exact order.
[abc] matches only three specific letters. [a-z] matches any single lowercase letter from a through z—a much wider set.
Character classes are core regex syntax since ES3. [abc] works in every browser and Node.js version.
Did you know?

You can merge a custom set with a range in one class: [abc0-9] matches a single character that is either a, b, c, or any digit. This keeps patterns compact when input draws from multiple small pools.

Continue to [^0-9] negated class

Learn how the caret inside brackets inverts a character class to match everything except digits.

[^0-9] 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