Regular Expression Cheatsheet
A fast reference to 46 regular-expression tokens, grouped into seven sections from anchors to escapes. Type in the search box to filter by token or by plain-English meaning, and every match shows what it does plus a short worked example. Each row has a Copy button so you can drop the token straight into your pattern.
How to use the regex cheatsheet
- Type in the search box to filter the sheet as you go.
- Search a token such as \d or a plain word such as digit or lookahead.
- Watch the count above the table to see how many tokens match.
- Read the Meaning column and the Example column for each row.
- Click Copy on a row to put that token on your clipboard.
Examples
Search the token \d
\d
2 tokens: \d (any digit, [0-9]) and \D (any non-digit), since search is case-insensitive
Search the word lookahead
lookahead
2 tokens: (?=...) positive lookahead and (?!...) negative lookahead
Search the word digit
digit
3 tokens: \d, \D and \w, because \w is defined as letters, digits or underscore
Frequently asked questions
What does \d mean in regex?
\d matches any single digit and is the same as the character class [0-9]. Its opposite, \D, matches any non-digit character. So \d\d matches a pair of digits like 42.
What is the difference between * and +?
Both repeat the token before them, but * matches zero or more, so it can match nothing at all, while + matches one or more and needs at least one occurrence. That is why ab*c matches ac, but ab+c does not.
What are greedy and lazy quantifiers?
By default * and + are greedy and grab as much as they can. Adding a ? makes them lazy so they match as little as possible, which is why <.*?> stops at the first > and matches only <a> in <a><b>.
What is a lookahead and a lookbehind?
A lookahead like (?=...) asserts a pattern follows the current spot without consuming it, and (?!...) asserts one does not. Lookbehinds (?<=...) and (?<!...) do the same for what comes before, so (?<=\$)\d+ matches 50 in $50.
What do regex flags like g, i, m and s do?
The g flag returns every match rather than just the first, i makes matching case-insensitive, m lets ^ and $ match at line breaks, and s (dotall) lets the dot match newlines too. The sheet also lists u for Unicode and y for sticky matching.
How do I match a literal special character?
Escape it with a backslash. So \. matches a real dot rather than any character, and \\ matches a single backslash, which is handy for Windows paths like C:\\path.
How does the search box match?
It does a case-insensitive substring match against both the token and its meaning, so searching digit also surfaces \w because its meaning mentions digits. Clearing the box brings back the full sheet grouped by category.
Are all the tokens valid in JavaScript regex?
Most are, but a couple are flagged as reference only. \A and \z are start and end of input anchors from other engines like PCRE and are not supported in JavaScript, where you use ^ and $ instead.
How do named groups and backreferences work?
(?<name>...) captures into a name you choose, and \k<name> refers back to it later in the pattern. Numbered backreferences also exist, so (\w)\1 matches a doubled letter such as the oo in book.
Learn more
- Greedy versus lazy quantifiers in regex
Why regex quantifiers grab as much as they can by default, how the lazy ? modifier reins them in, and when each behaviour is what you want.
Related tools
Regex Tester
Test a regular expression against text with live match highlighting, capture groups and flags. See every match and replace text. Runs in your browser.
Regex Explainer
Paste a regular expression to get a plain-English, token-by-token breakdown of what every part does, plus a summary and flag guide. Runs in your browser.
Regex Pattern Builder
Build ready-to-use regex for email, URL, IPv4, UUID, dates and more, or compose password rules with checkboxes. Copy the pattern. Runs in your browser.
.env to JSON
Convert a .env file to JSON, or JSON back to .env. Parses KEY=value lines, comments, quotes and export. Runs entirely in your browser.
ASCII Table
Full ASCII table for all 128 codes with decimal, hex, octal and binary values, character names and descriptions. Search by code, hex or character.
Aspect Ratio Box Generator
Generate CSS for a responsive aspect-ratio container. Use the modern aspect-ratio property or the padding-top fallback, then copy the ready code.