Regex Patterns & Flags
Regular expressions are built from small pieces that combine into powerful patterns. Understanding each building block gives you better control, fewer bugs, and more readable patterns.
Character Classes & Shorthand Tokens
Character classes let you match one character from a set. You can list characters inside square brackets [] or use shorthand tokens to cover common groups like digits and word characters.
Match digits and letters using character classes
const digitRegex = /\d+/g;
console.log('ID: 1234'.match(digitRegex)); // ['1234']
const wordRegex = /\w+/g;
console.log('Hello world!'.match(wordRegex)); // ['Hello', 'world']
const vowelRegex = /[aeiou]/gi;
console.log('Regex'.match(vowelRegex)); // ['e']Use character classes and shorthand tokens to match ranges of characters without listing each one individually.
\\d— any digit (0–9)\\w— word character (letter, digit, underscore)\\s— whitespace (space, tab, newline)[abc]— matcha,b, orc[^abc]— match anything excepta,b, orc
Quantifiers (How Many Times)
Quantifiers define how many times the previous token can repeat. By default, they are greedy (match as much as possible), but you can make them lazy to match as little as possible.
Use quantifiers to match repeated patterns
const input = 'A1 B22 C333';
console.log(input.match(/\d+/g)); // ['1', '22', '333']
// Match exactly 2 digits
console.log(input.match(/\d{2}/g)); // ['22']
// Match at least 2 digits
console.log(input.match(/\d{2,}/g)); // ['22', '333']Quantifiers like +, *, and {n,m} let you match repeated characters or groups. Use lazy quantifiers (e.g., *?) when you want the smallest match.
*— 0 or more times+— 1 or more times?— 0 or 1 time{n}— exactly n times{n,}— at least n times{n,m}— between n and m times*?,+?,??,{n,m}?— lazy quantifiers
Anchors & Boundaries
Anchors restrict where the pattern can match. They are particularly useful when validating that an entire string matches a pattern rather than just containing it.
Use anchors to validate full strings
const idRegex = /^[A-Z]{2}-\d{4}$/;
console.log(idRegex.test('AB-1234')); // true
console.log(idRegex.test('AB-12345')); // false
console.log(idRegex.test('xx AB-1234 yy')); // false^ and $ ensure that the entire string matches the pattern, not just a substring.
^— start of string (or line withmflag)$— end of string (or line withmflag)\\b— word boundary (between word and non-word characters)\\B— non-word boundary
Groups & Lookarounds
Groups capture parts of a match and allow you to apply quantifiers to multiple tokens at once. Lookarounds let you assert the presence or absence of a pattern without consuming characters.
Capture groups and replacements
const date = '2026-03-13';
const formatted = date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$1/$2/$3');
console.log(formatted); // 2026/03/13Capture groups (parentheses) allow you to refer to matched subgroups in replacements using $1, $2, etc.
Validate rules with lookahead
const password = /^(?=.*[A-Z])(?=.*\d)(?=.{8,}).*$/;
console.log(password.test('Test1234')); // true
console.log(password.test('test1234')); // falseLookahead assertions let you require multiple conditions without consuming characters, which is useful for complex validation.
(...)— capturing group(?:...)— non-capturing group(?=...)— positive lookahead(?!...)— negative lookahead(?<=...)— positive lookbehind (modern engines)(?<!...)— negative lookbehind
Escaping Special Characters
Certain characters have special meaning in regex. Escape them with a backslash \ to match them literally. For example, use \. to match a literal dot rather than any character.
. ^ $ * + ? ( ) [ ] | \— escape to match literally- Inside character classes, only
^,-,], and\are special, making it easier to include most symbols.
Next Steps
Ready to apply these patterns? Try the Regex Tester for real-time feedback, and visit the Regex Methods page to see how to use regex with JavaScript string APIs.