Regular Expressions (Regex) are algebraic patterns used for string search, input validation, text replacement, and data parsing across almost every programming language.
While regex patterns can appear cryptic at first glance (^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$), they follow deterministic rules.
This reference guide provides a developer cheatsheet for regex syntax, character classes, lookarounds, capture groups, performance pitfalls, and pattern generation tools.
1. Character Classes & Metacharacters
| Pattern | Description | Example Match |
| :--- | :--- | :--- |
| . | Any character except newline | a in "cat" |
| \d | Any digit [0-9] | 5 in "Item 5" |
| \D | Any non-digit character | I in "Item 5" |
| \w | Any word character [a-zA-Z0-9_] | x in "box_1" |
| \W | Any non-word character | @ in "user@" |
| \s | Any whitespace (space, tab, newline) | in "hello world" |
| \S | Any non-whitespace character | h in "hello world" |
2. Quantifiers (Controlling Match Length)
| Quantifier | Description | Example |
| :--- | :--- | :--- |
| * | 0 or more times (Greedy) | ba* matches "b", "ba", "baaa" |
| + | 1 or more times (Greedy) | ba+ matches "ba", "baaa" |
| ? | 0 or 1 time (Optional) | colou?r matches "color" & "colour" |
| {n} | Exactly n times | \d{4} matches "2026" |
| {n,} | n or more times | \d{2,} matches "12", "999" |
| {n,m} | Between n and m times | \d{2,4} matches "12", "1234" |
| *? | 0 or more times (Lazy) | ".*?" matches "first" in "first" "second" |
3. Anchors & Boundaries
Anchors assert position rather than matching physical characters:
^: Start of string or line (withmflag).$: End of string or line.\b: Word boundary (transition between\wand\W).\B: Non-word boundary.
4. Groups and Lookarounds
Capture Groups (...)
Captures matched sub-string into memory index $1, $2.
Non-Capturing Group (?:...)
Groups elements for quantifiers without saving memory index.
Lookahead & Lookbehind Assertions
- Positive Lookahead
(?=...): Matches if followed by pattern.- Example:
\d+(?= px)matches10in"10 px".
- Example:
- Negative Lookahead
(?!...): Matches if NOT followed by pattern.- Example:
\d+(?! px)matches10in"10 em".
- Example:
- Positive Lookbehind
(?<=...): Matches if preceded by pattern. - Negative Lookbehind
(?<!...): Matches if NOT preceded by pattern.
5. Common Production Regex Snippets
Email Address Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
URL Validation
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
IPv4 Address
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Hex Color Code
^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$
Test & Debug Regex Patterns Online
Test regular expressions, inspect capture groups, and generate pattern code for JavaScript, Python, and Go using the ToolzStack Regex Tester & Generator.