Regular Expressions Cheatsheet 2026
Interactive guide to Regex anchors, character classes, quantifiers, lookarounds, capturing groups, and search flags.
Interactive Skill Mastery
Mark commands as learned to build your customized reference tracker. Retained locally in this browser.
Anchors
When to Use
When you want to anchor your search pattern to a specific position in the string, like the absolute start, end, or word boundaries.
Common Mistakes
Forgetting to enable the multiline (m) flag if you want ^ and $ to match individual line starts inside a multi-line string block.
Shortcut / Pro-Tip
Combine ^ and $ for exact, strict match validation (e.g. /^abc$/ matches only the string 'abc' exactly).Example
const regex = /^/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When you want to anchor your search pattern to a specific position in the string, like the absolute start, end, or word boundaries.
Common Mistakes
Forgetting to enable the multiline (m) flag if you want ^ and $ to match individual line starts inside a multi-line string block.
Shortcut / Pro-Tip
Combine ^ and $ for exact, strict match validation (e.g. /^abc$/ matches only the string 'abc' exactly).Example
const regex = /$/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When you want to anchor your search pattern to a specific position in the string, like the absolute start, end, or word boundaries.
Common Mistakes
Forgetting to enable the multiline (m) flag if you want ^ and $ to match individual line starts inside a multi-line string block.
Shortcut / Pro-Tip
Combine ^ and $ for exact, strict match validation (e.g. /^abc$/ matches only the string 'abc' exactly).Example
const regex = /\b/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /\B/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)Character Classes
When to Use
When you want to match any character whatsoever except a newline boundary.
Common Mistakes
Forgetting that the dot is a special meta-character and needs to be escaped with a backslash (\.) when matching literal periods like in file extensions or domains.
Shortcut / Pro-Tip
Ensure 's' (dotall) flag is enabled if you want dot to match newline characters as well.Example
const dotMatch = /file.txt/.test("file-txt"); // Matches! Use /file\.txt/ for absolute strictness.Output Example
trueWhen to Use
When you need to detect or validate numeric digits (0 through 9) in input fields like phone numbers, zip codes, or IDs.
Common Mistakes
Assuming \d only matches ASCII digits; in some modern Unicode-aware engines, it can match non-ASCII numeral scripts too.
Shortcut / Pro-Tip
Equivalent to range: [0-9]Example
const hasDigit = /\d/.test("User_123");Output Example
trueWhen to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /\D/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /\w/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /\W/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /\s/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /\S/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /[a-z]/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /[^A-Z]/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)Quantifiers
When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /*/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /+/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /?/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /*?/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /{n}/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /{n,}/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /{n,m}/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)Groups
When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /(abc)/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /(?:abc)/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /(a|b)/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /\1/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)Assertions
When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /(?=abc)/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /(?!abc)/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /(?<=abc)/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
When specifying how many times the preceding character, class, or group must appear in the string.
Common Mistakes
Forgetting that quantifiers are greedy by default and will match the longest possible substring unless made lazy with '?'.
Shortcut / Pro-Tip
Use '{n,m}' for exact quantifier boundary validation instead of open wildcards.Example
const regex = /(?<!abc)/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)Flags
When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /g/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /i/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /m/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)When to Use
Use this regular expression pattern token to parse, search, or replace specific patterns inside text strings.
Common Mistakes
Writing over-complicated, unreadable patterns or forgetting to escape special punctuation characters.
Shortcut / Pro-Tip
Enable global (g) and multiline (m) flags to search through block text strings completely.Example
const regex = /s/;
const testStr = "test_data_2026";
const result = regex.test(testStr);Output Example
Result: true (Pattern matches standard alphanumeric sample inputs)Regex Best Practices
1Prefer Built-in String Parsers
Do not use Regex for simple searches. Native JS functions like .startsWith() or .includes() are significantly faster and easier to read.
2Use Lazy Quantifiers Wisely
Default quantifiers are greedy (e.g. .*). Use lazy quantifiers (e.g. .*?) to prevent matching past your target boundaries.
3Document Complex Patterns
Regex can quickly become unreadable. Write inline comments or use the 'x' flag (extended regex) where supported.
4Avoid Catastrophic Backtracking
Never nest open quantifiers like (a+)* inside patterns, which can cause quadratic search loops and lock web browsers.
5Test with Representative Edge Cases
Validate patterns against both valid inputs and malicious inputs to verify robustness.
Common Regex Errors & Solutions
Uncaught SyntaxError: Invalid regular expression: missing /
Check that your regex delimiters are properly balanced and that special chars like slashes are escaped (e.g. \/).
Pattern matches more text than expected (Greedy Match)
Append a question mark (?) after your quantifier to make the search non-greedy (lazy) (e.g. changed /<.*>/ to /<.*?>/).
Matches fail in multiline texts
Enable the multiline (m) flag so starting (^) and ending ($) anchors correspond to line breaks rather than string edges.
Special character matching fails
Ensure you escape special regex meta-characters (like . * + ? ^ $ ( ) [ ] { } | \) with a backslash to match literal values.
Search matches wrong cases
Ensure the case-insensitive (i) search flag is added or removed as required.
Common Regex Interview Questions
Q1What is the difference between greedy and lazy matching?
Greedy matching (+, *) matches the longest possible string that satisfies the pattern. Lazy matching (+?, *?) stops matching at the very first instance that satisfies the pattern.
Q2What is a capturing group vs. a non-capturing group?
A capturing group (abc) groups tokens and stores the matched text in memory for backreferences and match returns. A non-capturing group (?:abc) groups tokens without storing matched text, which optimizes search speeds.
Q3What is lookahead and lookbehind assertion?
Lookarounds are zero-width assertions. Lookahead (?=abc) matches a position only if it is followed by 'abc'. Lookbehind (?<=abc) matches only if it is preceded by 'abc', without including them in final match output.
Q4How do you escape characters in regular expressions?
Prepend a backslash (\) directly before any reserved meta-character (e.g., \. to match a literal dot, or \? to match a literal question mark).
Q5What does the word boundary anchor \b do?
The \b anchor matches a zero-width position between a word character (\w) and a non-word character (\W) or string boundaries, ensuring exact word matches.
Related Resources
JSON Formatter & Validator
Format, validate, and clean JSON payloads with syntax error detection.
REST API Tester
Send custom HTTP requests and test regular expression validations in response headers.
URL Encoder & Decoder
Safely encode or decode URLs to prevent parameter injection and parsing bugs.
Git Cheatsheet
Complete Git command reference for branching, merging, and tracking code.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com