Regular Expressions Tutorial
Learn Regular Expressions step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is a Regular Expression? Learn what regular expressions are, what problems they solve, how a regex engine reads a pattern, and when to choose a regex over plain string methods. Includes runnable examples.
- Regex in JavaScript: The Basics Use regular expressions in JavaScript and Node.js. Create RegExp objects with literals and the constructor, and use test, exec, match, matchAll, replace, replaceAll, search and split.
- Regex Flavors and Testing Tools Regular expressions come in flavors. Compare JavaScript, PCRE, Python, Java and POSIX syntax, see what each supports, and learn how to test patterns safely with small tools and checklists.
- Reading and Writing Your First Patterns A practical method for reading any regular expression piece by piece and for building your own, from sample text to a tested pattern, with worked examples for dates, times and IDs.
Syntax and Matching
- Literals, Escaping and the Dot Learn how literal characters match themselves, which characters are special in a regex, how to escape them with a backslash, and how the dot wildcard behaves with newlines and the s flag.
- Anchors and Word Boundaries Understand regex anchors and boundaries. Learn ^ and $, the m flag for line anchors, and the word boundaries \b and \B, with runnable examples for validation and whole-word search.
- Alternation and Precedence Learn how the regex alternation operator | chooses between options, why it has the lowest precedence, how to group alternatives with parentheses, and how ordering changes the result.
- Escape Sequences and Unicode Text Match tabs, newlines, control characters and any Unicode character with regex escape sequences. Learn \n, \t, \xHH, \uHHHH, \u{...}, the u flag, surrogate pairs and emoji pitfalls.
- Match Results, Indices and Iteration Read JavaScript regex match objects, loop through all matches with exec and matchAll, control lastIndex with the g and y flags, and get exact match positions with the d flag.
Character Classes and Quantifiers
- Character Classes Master regex character classes. Match one character from a set with [abc], use ranges like [a-z], negate with [^...], and learn the special rules for -, ^, ] and \ inside brackets.
- Shorthand Classes and Negation Learn the regex shorthand classes \d, \w and \s and their negations \D, \W and \S. See exactly what they match in JavaScript, where they surprise you, and how to build safer alternatives.
- Greedy Quantifiers Repeat patterns with the regex quantifiers ?, *, +, {n}, {n,} and {n,m}. Learn how greedy matching works, why it sometimes over-matches, and how to apply quantifiers to groups and classes.
- Lazy Quantifiers and Backtracking Basics Switch quantifiers to lazy mode with a trailing question mark, compare greedy and lazy matching, understand backtracking step by step, and learn when negated classes beat lazy quantifiers.
- Unicode Property Escapes Match letters, digits, punctuation, emoji and whole scripts from any language with the regex \p{...} and \P{...} property escapes in JavaScript. Includes categories, scripts and practical examples.
Groups, Backreferences and Lookaround
- Capturing Groups Use parentheses in regular expressions to group tokens and capture text. Learn group numbering, nested groups, quantified groups, optional groups and how to read captures in JavaScript.
- Named and Non-Capturing Groups Make regexes readable with named capture groups and avoid unneeded captures with non-capturing groups. Covers groups objects, destructuring, duplicate names, replacement references and modifiers.
- Backreferences Match the same text twice with regex backreferences. Learn \1 and \k<name>, find repeated words and matching quotes, understand forward and unset references, and see what regular expressions cannot count.
- Lookahead Use positive and negative lookahead in regular expressions to check what follows without consuming it. Includes password rules, number formatting, excluding words and an atomic-group trick.
- Lookbehind Use positive and negative lookbehind in JavaScript regular expressions to check what comes before a position. Covers variable-length lookbehind, currency and prefix examples, and engine differences.
- Replacing with Groups and Functions Transform text with regex replace. Use replacement patterns $1, $&, $<name> and $$, write replacer functions with captures and offsets, and build safe templates, reformatters and sanitizers.
Flags and Engines
- Regex Flags in Depth Learn what each regular expression flag does, from global and case-insensitive matching to multiline, dotAll, unicode, sticky and indices, with runnable JavaScript examples.
- Unicode and the v Flag Match text in any language with Unicode regular expressions. Learn code points versus code units, property escapes like \p{L}, case folding, and set operations with the v flag.
- Regex Flavors and Engines Understand why the same regular expression behaves differently across languages. Compare backtracking and automaton engines, the PCRE, JavaScript, Python, Java and RE2 flavors, and portable syntax.
- Sticky Matching and Match Indices Use the sticky flag and lastIndex to build tokenizers, and the d flag to get exact start and end positions of every capture group. Includes a tiny lexer you can run.
Patterns for Text Processing
- Validating Input with Regex Learn how to validate form input with regular expressions the right way, using anchors, clear structure and honest limits, with patterns for usernames, hex colors, dates, IDs and passwords.
- Extracting Data with Regex Pull structured data out of free text with regular expressions. Use capture groups, named groups and matchAll to extract emails, hashtags, prices, dates and key-value pairs.
- Search and Replace Recipes Master regex replacement. Learn $1, $<name> and $& substitutions, replacer functions, replaceAll, and practical recipes for case conversion, trimming, masking, reformatting and escaping.
- Splitting and Tokenizing Text Use regular expressions to split strings, keep delimiters with capture groups, tokenize text into words and numbers, parse CSV lines with quotes, and break paragraphs into sentences.
Performance and ReDoS
- How Backtracking Works See how a backtracking regex engine really matches text. Follow greedy and lazy quantifiers, alternation order and failure paths step by step, and learn to count the work a pattern does.
- Catastrophic Backtracking and ReDoS Learn how nested quantifiers and overlapping alternations cause exponential regex time, how ReDoS attacks take servers down, and how to recognize and fix evil patterns safely.
- Optimizing and Hardening Regex Practical techniques to make regular expressions fast and safe, from anchors and specific classes to atomic-group emulation, input limits, worker-thread timeouts and choosing linear-time engines.
Regex in Languages and Tools
- Regex in JavaScript: The Complete API A complete tour of regular expressions in JavaScript: RegExp objects, test, exec, String match, matchAll, search, replace, split, named groups, lastIndex, and common pitfalls.
- Regex in Other Languages See the same regular expression tasks in Python, PHP and Java, with runnable code and a cheat sheet for Go, Rust, C#, Ruby and shell tools, and learn which syntax differences cause most bugs.
- Regex on the Command Line Use regular expressions in the shell with grep, sed and awk. Learn basic versus extended syntax, POSIX classes, common options, and recipes for filtering, extracting and rewriting log and text files.
Projects and Practice
- Project: A Log Analyzer Build a log analyzer step by step with regular expressions. Parse web server access lines with named groups, count status codes, rank paths, find slow requests and flag suspicious clients.
- Project: Markdown to HTML Converter Build a small Markdown to HTML converter with regular expressions. Escape HTML safely, convert headings, lists, code, emphasis, links and paragraphs, and learn where regex stops being enough.
- Project: A Template Engine Build a small template engine with regular expressions. Tokenize text and tags, resolve dotted paths, apply filters, escape output, and add if and for blocks with a recursive parser.
- Regex Practice Challenges Sharpen your regular expression skills with twelve graded challenges covering anchors, classes, groups, backreferences, lookaround and replacement. Edit the patterns, run the tests, then compare with the solutions.