What Is a Regular Expression? Regex Explained
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex is used to validate input, search and replace text, parse structured strings (URLs, dates, log lines), and extract data. Nearly every programming language has built-in regex support.
Core Syntax
. matches any character. \d digit. \w word character (a-z, A-Z, 0-9, _). \s whitespace. ^ start of string. $ end of string. [abc] character class. [^abc] negated class. | alternation (or). \ escapes a special character. Quantifiers: * (0 or more), + (1 or more), ? (0 or 1), {n} (exactly n), {n,m} (between n and m). Greedy vs lazy: .+ is greedy (matches as much as possible); .+? is lazy (matches as little as possible).
Groups and Capturing
Parentheses create capturing groups: (\d{4})-(\d{2})-(\d{2}) captures year, month, day from a date string. (?:...) is a non-capturing group (for grouping without capturing). Named groups: (?<year>\d{4}) — accessible as match.groups.year in JavaScript. Backreferences: \1 refers to the first captured group — useful for finding repeated words like (\w+) \1.
Lookaheads and Lookbehinds
(?=...) positive lookahead: matches if followed by the pattern. (?!...) negative lookahead. (?<=...) positive lookbehind: matches if preceded by the pattern. (?<!...) negative lookbehind. Example: \d+(?= USD) matches numbers followed by USD without including USD in the match. Lookarounds are zero-width assertions — they don't consume characters.