[abc]
Matches one character from the set.
Character classes describe allowed characters. Assertions describe positions without consuming characters.
const pattern = /^#[0-9a-f]{6}$/i;
console.log(pattern.test("#ffcc00"));
Regular Expressions
Character classes such as [a-z], \d and \s match categories of characters. Negated classes such as [^0-9] match anything except that set.
Assertions such as ^, $, \b and lookarounds do not consume characters. They assert where a match may happen.
Classes and assertions are the building blocks of useful validation patterns, but they should remain readable and well-scoped.
Matches one character from the set.
Common shorthand classes for digits, word characters and whitespace.
Start and end anchors.
Word boundary assertion.
Examples
const hexPattern = /^#[0-9a-f]{6}$/i;
hexPattern.test("#ffcc00");
const hexPattern = /#[0-9a-f]{6}/i;
hexPattern.test("bad #ffcc00 extra"); // true
Code patterns
These examples are intentionally small. Regex becomes useful when each pattern has one clear job and a readable method around it.
Class ranges with anchors and a quantifier.
const hexPattern = /^#[0-9a-f]{6}$/i;
Use \d with full-string anchors.
const numberPattern = /^\d+$/;
Match whole words, not substrings.
const pattern = /\bcss\b/i;
Disallow a set of characters.
const noSpaces = /^[^\s]+$/;
Rules that matter
Regular expressions are compact by design. Good JavaScript around the pattern is what makes them safe to maintain.
[a-z] and \d are character rules.
Otherwise a valid fragment can pass inside invalid text.
\b avoids matching inside longer words.
It is not the same as every letter in every language.
They are powerful but can obscure intent.
Validation regexes need invalid examples too.
Production thinking
Classes and assertions decide whether a regex is precise or accidentally permissive.
Validation should explain which characters are allowed instead of only saying invalid.
Production regex validation should include tests for valid and invalid examples.
Slug and color-style validation can protect clean URLs and structured front-end data.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Regex skill is not memorizing every token. It is knowing how to build small patterns and choose the right string method around them.