Regular expressions (regex or regexp) are one of the most powerful tools in a programmer's toolkit — and one of the most feared. A well-crafted regex can validate an email address, parse a log file, or extract data from a string in a single, compact expression. This guide builds up regex knowledge from zero, with practical patterns you can use immediately.
> Test patterns live: Use our free [Regex Tester](/tools/regex-tester) to build, test, and debug regular expressions with real-time match highlighting — no setup needed.
What is a Regular Expression?
A regular expression is a sequence of characters that defines a search pattern. It can be used to match, search, replace, or split strings. Almost every programming language supports regex: JavaScript, Python, Java, PHP, Ruby, Go, and more.
In JavaScript, a regex is written between forward slashes: /pattern/flags. For example, /hello/i matches "hello", "Hello", "HELLO" and any other capitalisation.
Core Syntax: Literals and Metacharacters
Advertisement
Literals: Most characters match themselves. The pattern /cat/ matches the string "cat" anywhere in the text.
Metacharacters: Special characters with regex-specific meaning: . ^ $ * + ? { } [ ] \ | ( )
To match a literal metacharacter, escape it with a backslash: /\./ matches a period; /\$/ matches a dollar sign.
Character Classes
A character class [...] matches any one character from the listed set:
[abc]— matches 'a', 'b', or 'c'[a-z]— matches any lowercase letter[A-Z]— matches any uppercase letter[0-9]— matches any digit[a-zA-Z0-9]— matches any alphanumeric character[^abc]— matches any character NOT in the set (the ^ negates inside[])
Shorthand character classes:
\d— digit (equivalent to[0-9])\w— word character (letters, digits, underscore:[a-zA-Z0-9_])\s— whitespace (space, tab, newline).— any character except newline
Anchors
Anchors match positions, not characters:
^— start of string (or start of line in multiline mode)$— end of string (or end of line in multiline mode)\b— word boundary\B— not a word boundary
Examples:
/^Hello/matches strings starting with Hello/World$/matches strings ending with World/\bcat\b/matches the word "cat" but not "tomcat" or "category"
Quantifiers
Quantifiers control how many times a pattern can repeat:
*— 0 or more times+— 1 or more times?— 0 or 1 time (makes the preceding element optional){n}— exactly n times{n,m}— between n and m times
Examples:
/\d+/matches one or more digits/^\d{5}$/matches exactly 5-digit strings (US ZIP codes)/colou?r/matches both "color" and "colour" (the u is optional)
Greedy vs. Lazy: By default, quantifiers are greedy — they match as much as possible. Adding ? makes them lazy (matching as little as possible):
- Greedy:
/<.*>/on<b>Bold</b>matches the entire string<b>Bold</b> - Lazy:
/<.*?>/matches only<b>
Groups and Alternation
Parentheses () create capturing groups:
const match = '2024-12-25'.match(/(\d{4})-(\d{2})-(\d{2})/)
// match[1] = '2024', match[2] = '12', match[3] = '25'Non-capturing groups (?:...) group without capturing.
Alternation | means "or": /cat|dog/ matches either "cat" or "dog".
Flags
i— case-insensitiveg— global (find all matches, not just the first)m— multiline (^and$match line boundaries)s— dotAll (.matches newline characters)
8 Practical Regex Patterns You Can Use Today
# Email (simplified)
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
# Date YYYY-MM-DD
/^\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/
# Hex colour
/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
# Strong password (min 8 chars, letter, number, symbol)
/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$/
# IPv4 address
/^(\d{1,3}\.){3}\d{1,3}$/
# URL
/^https?:\/\/[^\s/$.?#].[^\s]*$/
# Duplicate words
/\b(\w+)\s+\1\b/gi
# Non-empty string (not just whitespace)
/^(?!\s*$).+/Copy any of these and test them instantly in our [Regex Tester](/tools/regex-tester) with your own input data.
Common Regex Mistakes
Not escaping periods: In a regex, . matches any character. To match a literal period (like in a file extension), use \. instead.
Greedy matching: /<.*>/ intended to match one HTML tag often matches everything from the first opening bracket to the last closing bracket. Use a lazy quantifier (/<.*?>/).
Anchoring: A pattern without ^ and $ anchors can match anywhere in the string. /\d+/ matches "abc123def" at the digit position. /^\d+$/ requires the entire string to be digits.
Not testing edge cases: Always test your regex against empty strings, very long inputs, and strings with special characters.
Testing and Debugging Regex
Regular expressions are notoriously tricky to get right on the first try. Always test with:
- Strings that should match
- Strings that should not match
- Edge cases (empty string, very long input, special characters, Unicode)
Our [Regex Tester](/tools/regex-tester) lets you enter a pattern and test input in real time, with colour-coded match highlighting, capture group extraction, and error reporting.
When Not to Use Regex
Regex is not the right tool for every parsing problem:
- Do not use regex to parse HTML or XML — use a proper DOM parser
- Do not use regex to validate complex formats that have dedicated parsers (dates with timezone awareness, full URL validation, emails with international domains in production)
- Do not use regex for very complex nested structures — a parser or grammar is more appropriate
If your regex is over 80 characters long and hard to read, consider whether a dedicated parser or a few lines of simple string logic might be clearer.
Frequently Asked Questions
Q: What is the difference between `/g` and `/gi` flags?
/g (global) finds all matches in the string instead of stopping at the first. /i (case-insensitive) makes the match ignore uppercase/lowercase. /gi combines both — useful for patterns like /error/gi to find all mentions of "error" regardless of capitalisation in a log file.
Q: How do I match a newline with regex?
In JavaScript, . does not match newlines by default. Use the s flag to enable dotAll mode (/pattern/s), or use [\s\S] instead of . to explicitly match any character including newlines.
Q: Why does my regex work in the tester but not in my code?
The most common reason is differences in how the language handles string escaping. In a JavaScript string literal, you write \\d to get the regex pattern \d. In a regex literal (/\d/), you write it directly. Use our [Regex Tester](/tools/regex-tester) and copy the pattern directly as a regex literal.
Q: How do I extract text between two strings?
Use capturing groups with the content in the middle: /START(.*?)END/s. The s flag makes . match newlines, and ? makes it non-greedy so it stops at the first END rather than the last.
Conclusion
Regular expressions reward investment in learning. Start with the core syntax — character classes, quantifiers, anchors — and build from there. Use a regex tester to iterate quickly, and reference pattern libraries for common validations rather than writing complex patterns from scratch. [ToolHub's Regex Tester](/tools/regex-tester) provides instant feedback as you build and debug patterns — the fastest way to master regex.