Concepts
The building blocks of regex — what each syntax element does and when to reach for it.
Anchors: ^ and $ Explained
Anchors match positions, not characters. ^ asserts the start of the string (or line, with the m flag) and $ asserts the end.
Links to 3 patterns
Word Boundaries: \b and \B
\b matches the position between a word character and a non-word character. It keeps your regex from matching 'cat' inside 'concatenate.'
Links to 4 patterns
Character Classes: \d, \w, \s and Their Negations
Shorthand character classes match broad categories of characters. \d is a digit, \w is a word character, \s is whitespace. Uppercase negates.
Links to 4 patterns
Custom Character Sets: [abc], [a-z], [^abc]
Square brackets build a custom character class. [abc] matches any of a, b, or c. [a-z] is a range. A leading ^ negates the set.
Links to 4 patterns
Quantifiers: *, +, ?, and {n,m}
Quantifiers repeat the preceding element. * is 0 or more, + is 1 or more, ? is 0 or 1. {n}, {n,}, and {n,m} give exact counts and ranges.
Links to 4 patterns
Lazy vs. Greedy Quantifiers
Greedy quantifiers (*, +) consume as much as possible before backtracking. Lazy quantifiers (*?, +?) consume as little as possible.
Links to 4 patterns
Capturing Groups and Non-Capturing Groups
Parentheses group tokens and capture the matched substring. (?:...) groups without capturing — use it when you want grouping for quantifiers or alternation but don't need the submatch.
Links to 4 patterns
Lookahead and Lookbehind
Lookarounds assert a condition at a position without consuming characters. (?=X) is lookahead, (?<=X) is lookbehind. ! negates them.
Links to 1 pattern
Alternation with the | Operator
The pipe | matches either the expression on its left or on its right. Combine with groups to alternate over a subpattern.
Links to 4 patterns
Regex Flags in JavaScript: g, i, m, s, u, y
Flags modify regex behavior globally. g enables global matching, i makes it case-insensitive, m changes anchor behavior, s dots match newlines, u enables Unicode, y is sticky.
Links to 4 patterns
How-to guides
Task-oriented recipes for the most common regex jobs — extracting, validating, replacing, and matching specific shapes.
How to Match Digits in Regex
Use \d for any digit, [0-9] for ASCII digits only, or {n} to match a specific count of digits. Combine with anchors for whole-string validation.
Links to 4 patterns
How to Match Whitespace in Regex
\s matches any whitespace — space, tab, newline, and more. Use \s+ to collapse runs, ^\s*$ to detect blank lines, and \S for non-whitespace.
Links to 2 patterns
How to Extract URLs from Text with Regex
Use a permissive URL pattern with the g flag and String.matchAll. A practical regex accepts http/https, optional www, and common URL characters.
Links to 2 patterns
How to Validate an Email with Regex
Use a pragmatic regex for structural validation, then confirm deliverability separately. Full RFC 5322 compliance is overkill and error-prone.
Links to 1 pattern
How to Match Across Newlines in JavaScript
The dot doesn't match newlines by default. Use the s (dotall) flag, or build an explicit [\s\S] alternative for engines that predate s.
Links to 3 patterns
How to Match an Optional Group
Put a ? after a character, group, or character class to make it optional. Groups with ? match zero or one occurrence of the whole group.
Links to 2 patterns
How to Replace Text Using Regex in JavaScript
String.replace and String.replaceAll accept a regex. Use the g flag for multi-match replace, $1/$2 for capture references, and a function for complex logic.
Links to 2 patterns
How to Escape Regex Special Characters
Backslash-escape any of .^$*+?()[]{}|\ to match them literally. When building a regex from user input, use a full-escape helper function.
Links to 3 patterns
How to Match a Specific Number of Characters
Use {n} for exactly n, {n,} for n or more, {n,m} for between n and m. Apply to any single token — character, class, or group.
Links to 4 patterns
How to Match Multiple Patterns (Alternation)
Combine patterns with | to match either option. Wrap alternatives in a group to scope the alternation correctly.
Links to 3 patterns
Try any regex live
RegexPro's interactive tester highlights matches in real time — no sign-up, no installs.
Open Regex Tester