Regular Expressions for Beginners: Syntax Cheat Sheet
Basics4 minInnovialabs editorial team
In short
A regular expression is a text-matching pattern built from character classes, quantifiers, anchors and groups; learn those four building blocks and you can read most regexes.
What a regular expression is
A regular expression (regex) is a short pattern that describes the text you are looking for. It is used to validate input formats, extract data from strings, and search and replace in code and logs.
Almost every regex is built from four blocks:
- what to match: characters and character classes;
- how many times: quantifiers;
- where: anchors and boundaries;
- how to group: groups and alternation.
Syntax in JavaScript, Python, PHP, Java and Go is mostly the same, but details differ, so check your language's documentation.
Characters and character classes
| Pattern | Meaning | Example match |
|---|---|---|
abc | literally "abc" | abc |
. | any character except a newline | a, 7, % |
\d | a digit | 5 |
\w | a letter, digit or _ | a, Z, 3, _ |
\s | a whitespace character | space, tab |
\D, \W, \S | anything except \d, \w, \s | |
[abc] | one of a, b, c | b |
[a-z] | a character in the range | k |
[^0-9] | any character except digits | x |
\. | a literal dot, not "any" | . |
The special characters . * + ? ^ $ ( ) [ ] { } | and the backslash must be escaped with \ when you want them literally.
Note that in some engines \w and \d match only Latin letters and ASCII digits by default. For Cyrillic or other scripts, an explicit class or Unicode mode is more reliable.
Quantifiers
| Pattern | How many times |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{3} | exactly 3 |
{2,5} | 2 to 5 |
{2,} | 2 or more |
Quantifiers are greedy by default and grab as much as possible. Add ? to make them lazy: on <b>text</b>, <.+> matches the whole string, while <.+?> matches only <b>.
Anchors and boundaries
| Pattern | Meaning |
|---|---|
^ | start of the string |
$ | end of the string |
\b | word boundary |
\B | not a word boundary |
For validation you almost always need ^ and $. The pattern \d{4} finds 4 digits inside "abc12345", while ^\d{4}$ accepts only a string of exactly four digits.
Groups and alternation
| Pattern | Meaning |
|---|---|
(abc) | capturing group |
(?:abc) | non-capturing group |
a|b | a or b |
\1 | backreference to the first group |
(?<name>...) | named group (in Python, (?P<name>...)) |
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const m = "Date: 2026-03-15".match(re);
console.log(m.groups.year); // "2026"
Use (?:...) when a group exists only for a quantifier or alternation, so your capture numbering stays intact.
Lookahead and lookbehind
Lookarounds inspect neighboring text without including it in the match.
| Pattern | Meaning |
|---|---|
X(?=Y) | X if followed by Y |
X(?!Y) | X if not followed by Y |
(?<=Y)X | X if preceded by Y |
(?<!Y)X | X if not preceded by Y |
A classic example is a password check: ^(?=.*\d)(?=.*[a-z]).{8,}$ means at least 8 characters, at least one digit and one lowercase letter. Lookbehind is not supported in every engine, and some restrict its length.
Flags
i: case-insensitive;g: find all matches (JavaScript);m:^and$apply to each line of the text;s: the dot also matches a newline;u: Unicode mode (JavaScript).
How to test your regexes
- Build the pattern in an online tester such as regex101 or RegExr: they highlight matches and explain every part.
- Select the same engine your language uses.
- Collect strings that should and should not match, including the empty string and edge cases.
- Move that list into unit tests so the regex does not break during later edits.