What a regex is
A regular expression (regex) is a small pattern language for finding and checking text: "a word that starts with a capital letter", "five digits, a dash, three digits", "everything between two tags". Almost every language and editor supports it with nearly the same syntax. Learn ten symbols and you can read most patterns; the rest is practice. Every example below was tested in JavaScript, and you can try them in the regex tester, which highlights matches and groups as you type.
The building blocks
| Pattern | Matches |
|---|---|
. | any character except a line break |
\d \w \s | a digit, a word character (letter, digit, _), whitespace |
[abc] [a-z] [^0-9] | one of the listed characters, a range, anything but |
* + ? | zero or more, one or more, zero or one |
{3} {2,5} | exactly 3, from 2 to 5 |
^ $ | start and end of the text (or of a line with the m flag) |
\b | a word boundary |
(…) (?:…) (?<name>…) | a capturing group, a non-capturing group, a named group |
| a vertical bar between two patterns | either one: cat or dog |
\. \( | a literal dot or parenthesis (escaped) |
The regex cheat sheet keeps the full list one click away.
Tested examples
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test('ana@exemplo.com.br'); // true
/^\d{5}-?\d{3}$/.test('01310-100'); // true: Brazilian CEP
/^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$/.test('21/11/2026'); // true
'2026-11-21'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1'); // '21/11/2026'Notice what they do not do: the e-mail pattern only rejects obvious typos (real validation is sending a message), and the date pattern accepts 31/02/2026, because a regex checks shape, not calendars. Check the values in code after the match.
Greedy and lazy
Quantifiers are greedy: they take as much as they can. On <b>a</b><b>c</b>, the pattern <b>.*</b> matches the whole string; adding ? makes it lazy, so <b>.*?</b> stops at the first </b> and matches <b>a</b>. For HTML beyond small snippets, use a parser instead of a regex.
Flags and engine differences
- Flags in JavaScript:
g(all matches),i(ignore case),m(^and$per line),s(.matches line breaks),u(Unicode, needed for emoji and\p{L}). - Python's
rewrites named groups as(?P<name>…); JavaScript and PCRE accept(?<name>…). - PCRE (PHP) and Python 3.11+ have possessive quantifiers and atomic groups; JavaScript does not.
- Patterns like
^(a+)+$can take exponential time on inputs such asaaaaaaaaaaaaaaaaaaaaaaaaaaaa!(catastrophic backtracking). Avoid nested quantifiers on the same characters, and never run user-supplied patterns on a server without a time limit. To clean lists line by line without writing a pattern, try the line tools.
FAQ
Why does my regex match too much?
A greedy .* or .+ ran to the last possible match. Make it lazy (.*?) or use a narrower class such as [^<]*.
How do I match a literal dot or bracket?
Escape it with a backslash: \., \[, \(. Inside a character class most symbols are literal: [.] is a dot.
Can a regex validate a CPF or a credit card?
Only the layout. The check digits need arithmetic; see CPF and CNPJ check digits.
Why does the same pattern behave differently in another language?
Engines differ in flags, named-group syntax, lookbehind support and Unicode handling. Test in the engine you will run.