A beginner-friendly guide to regex. Learn character classes, quantifiers, anchors, flags (g, i, m), capture groups, and battle-tested validation patterns.
Calculate your numbers instantly in your browser. Fast, free, no signup.
A famous programming adage by Jamie Zawinski states: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems."
While regular expressions (Regex) have an intimidating reputation for looking like cryptic line noise, they are one of the most powerful tools in a developer's arsenal for searching, validating, extracting, and manipulating text strings.
. : Matches any character except newline.\d : Matches any digit ([0-9]).\D : Matches any non-digit.\w : Matches any word character (alphanumeric + underscore [a-zA-Z0-9_]).\W : Matches any non-word character.\s : Matches any whitespace character (space, tab, newline).[abc] : Matches any character inside the brackets.[^abc] : Matches any character not inside the brackets (negated set).* : 0 or more occurrences (greedy).+ : 1 or more occurrences.? : 0 or 1 occurrence (optional).{3} : Exactly 3 occurrences.{2,5} : Between 2 and 5 occurrences.^ : Start of string (or start of line in multiline mode).$ : End of string (or end of line in multiline mode).\b : Word boundary (transitions between word and non-word characters).Flags change how the regular expression engine interprets the pattern:
g (Global): Find all matches rather than stopping after the first occurrence.i (Case-Insensitive): Ignore uppercase vs. lowercase distinctions (/hello/i matches "Hello" and "HELLO").m (Multiline): Makes ^ and $ match the beginning and end of each line, not just the entire string.s (DotAll): Allows . to match newlines as well.Parentheses (...) define a capturing group, allowing you to extract specific substrings or reference them during find-and-replace:
const datePattern = /(\d{4})-(\d{2})-(\d{2})/;
const result = "2026-09-22".replace(datePattern, "$2/$3/$1");
// Output: "09/22/2026"
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches valid email formats like user.name+tag@example.co.uk.
^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requires at least 8 characters, one lowercase letter, one uppercase letter, one digit, and one special character.
Writing regex without immediate visual feedback leads to endless trial and error. Test your regular expressions, view live color-coded matches, and inspect capture groups with our free Regex Tester.