JavaScript RegExp [abc] Character Class

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

What Is [abc]?

[abc] is a character class that matches one character from a custom set — here a, b, or c. Order inside the brackets does not matter.

Remember
/[abc]/     → one of: a, b, or c
/[abc]+/    → one or more from that set
/[abc]/i    → case-insensitive (also A, B, C)

[abc]  ≠  (abc)
set of one char    vs    literal "abc"
UsePrefer
One of a few letters/[abc]/
Any lowercase letter/[a-z]/ (range)
Not those letters/[^abc]/

How to Use [abc]

List the allowed characters inside [ ], then test or extract.

  1. Put the allowed characters between square brackets: [abc].
  2. Add a quantifier (+, {n}) if you need more than one match in a row.
  3. Use test() for yes/no, or exec() / match() to read the character.
  4. Remember: this is a set, not the word "abc" — use (abc) for that sequence.
PieceRequired?Role
[abc]YesMatches one character from the set
QuantifierOptional+, {2}, etc. for length
i flagOptionalIgnore case for the letters

Pseudocode

Pseudocode
pattern = /[abc]/          // or /[abc]+/, /[abc]/i
str = text to check

if pattern.test(str):
    // at least one of a, b, c found

match = pattern.exec(str)
// match[0] is the character (or run)

Cheat Sheet

Keep these patterns handy while matching a small set of characters.

PatternMatches
/[abc]/One of a, b, c
/[abc]+/One or more from that set
/[abc]/iAlso A, B, C
/^[abc]$/Whole string is one of those letters
/[a-z]/Any lowercase letter (range)
/[^abc]/One character that is not a, b, or c

Live Preview

Pick a sample string and see whether /[abc]/ finds a match.

Browser demo of /[abc]/ with test and exec.

Result
str = "cab"
/[abc]/.test(str) → true
exec → "c" (index 0)

JavaScript Examples

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

Example 1 — Match One From the Set

Use test() and exec() with /[abc]/.

JavaScript
const set = /[abc]/;

console.log(set.test("cab"));   // true
console.log(set.test("dog"));   // false
console.log(set.exec("xby")[0]); // "b"
Try It Yourself

How It Works

  1. Define the set. /[abc]/ matches one of a, b, or c.
  2. Hit. "cab" starts with c → true.
  3. Miss. "dog" has none of those letters → false.
  4. Read it. exec("xby") returns the first match "b".

Example 2 — One or More From the Set

Add + to grab a run of allowed letters.

JavaScript
const run = /[abc]+/g;
const str = "ab and cab";

console.log(str.match(run));
// ["ab", "cab"]
Try It Yourself

How It Works

  1. Quantifier +. [abc]+ means one or more letters from the set in a row.
  2. Global flag. g finds every run in the string.
  3. match. Returns ["ab", "cab"].
  4. Spaces break runs. Letters outside the set stop the current match.

Example 3 — Case-Insensitive Set

Use the i flag so uppercase letters count too.

JavaScript
const set = /[abc]/i;

console.log(set.test("Apple")); // true  (A)
console.log(set.test("dog"));   // false
console.log(set.exec("XB")[0]); // "B"
Try It Yourself

How It Works

  1. Default is case-sensitive. Without i, A would not match [abc].
  2. i flag. /[abc]/i also accepts A, B, and C.
  3. Apple. The leading A matches → true.
  4. Still a set. Letters outside a/b/c (and A/B/C) still fail.

Edge Cases & Pitfalls

Check these before treating [abc] like a word match.

trap

Set ≠ sequence

[abc] matches one letter. (abc) matches the literal word "abc".

case

Case-sensitive by default

A does not match [abc] unless you add i or list A in the brackets.

length

One character by default

Use + or {n} when you need a run of letters from the set.

negate

[^abc] is the opposite

A leading ^ inside brackets means “not these.” See [^abc].

Browser & Runtime Support

Character classes like [abc] are a core part of JavaScript regular expressions — available in every environment.

✓ Baseline · Widely available

RegExp [abc]

Supported in all browsers and Node.js versions since early ECMAScript. No polyfill needed.

100% Universal support
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] Universal

Bottom line: Use freely. List only the characters you need; use ranges like [a-z] when the set is large.

Performance Notes

ApproachCostBest for
/[abc]/O(n) scanDetect any set letter
/[abc]+/gO(n) scanExtract runs from the set
/^[abc]$/O(1)–O(n)Single-letter codes

Small character classes are cheap. Prefer a clear set ([abc]) over listing alternations like a|b|c when matching one character.

Key Takeaways

  • Role: [abc] matches one character from a custom set.
  • Not a word: use (abc) for the literal sequence "abc".
  • Length: add + or {n} for runs from the set.
  • Case: default is sensitive — use i or list both cases.

One line: [abc] means “one of these characters” — not the word abc.

Frequently Asked Questions

[abc] is a character class that matches exactly one character — either a, b, or c. Order inside the brackets does not matter: [abc] is the same as [cab].
Not by itself. Each [abc] matches one character. Use [abc]+ for one or more letters from the set, or [abc]{3} for exactly three.
Yes. [abc] matches lowercase a, b, and c only. Include uppercase explicitly ([abcABC]) or use the i flag when that fits your pattern.
[abc] is a character class matching one letter from the set. (abc) is a capturing group matching the literal sequence "abc" in that order.
[abc] matches only three specific letters. [a-z] matches any single lowercase letter from a through z.
Character classes are core regex syntax since early ES. [abc] works in every browser and Node.js version.

Next: [^0-9]

Learn the negated digit class — match anything that is not a digit.

[^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.

9 people found this page helpful