Regular expressions are one of the most transferable skills in programming. A beginner-friendly guide covering all core syntax with practical examples and tips for using a visual regex tester.
Regular expressions look intimidating — a string like /^[\w.-]+@[\w.-]+\.\w{2,}$/bears no resemblance to anything in natural language. But once you understand the rules, regex becomes an extraordinarily powerful tool for matching, extracting, and transforming text. This guide takes you from zero to writing useful patterns.
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex engines use these patterns to:
Regex is supported natively in almost every programming language — JavaScript, Python, PHP, Java, Ruby, Go, and more — with only minor syntax differences between them.
cat matches the string “cat” anywhere in the text.. (dot) — Matches any single character except a newline.c.t matches “cat”, “cut”, “c3t”.^ — Matches the start of the string (or line in multiline mode).$ — Matches the end of the string.* — 0 or more of the preceding character.ab*c matches “ac”, “abc”, “abbc”.+ — 1 or more of the preceding character.ab+c matches “abc”, “abbc” but NOT “ac”.? — 0 or 1 of the preceding (makes it optional).colou?r matches both “color” and “colour”.[abc] character class — Matches any one of the characters in the brackets. [aeiou] matches any vowel.[^abc] negated class — Matches any character NOT in the brackets.\d — Matches any digit (0–9). Equivalent to[0-9].\w — Matches word characters: letters, digits, and underscore. Equivalent to [a-zA-Z0-9_].\s — Matches whitespace: space, tab, newline.{n,m} — Matches between n and m repetitions.\d{2,4} matches 2 to 4 consecutive digits.(group) — Captures a group for extraction or backreferences.a|b — Alternation: matches either a or b.g (global — find all matches), i(case-insensitive), m (multiline).| Pattern | What it matches |
|---|---|
^\d{4}-\d{2}-\d{2}$ | ISO date format (YYYY-MM-DD) |
[\w.-]+@[\w.-]+\.\w{2,} | Email address (simplified) |
https?://[^\s]+ | HTTP or HTTPS URL |
#[0-9A-Fa-f]{6} | 6-digit hex colour code |
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b | IPv4 address (simplified) |
^(?=.*[A-Z])(?=.*\d).{8,}$ | Password: 8+ chars, at least one uppercase, one digit |
Regex is one of the most transferable skills in programming — learn it once and use it everywhere. The ToolsGravity Regex Tester gives you immediate visual feedback on your patterns with highlighted matches and capture group extraction. Start with simple literal patterns, add quantifiers, then build up to character classes and groups. Most real-world validation and extraction tasks require only a small subset of the full regex syntax.