Programming Utility
Updated for 2026

Regular Expressions Cheatsheet 2026

Interactive guide to Regex anchors, character classes, quantifiers, lookarounds, capturing groups, and search flags.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 32 Mastered (0%)

Anchors

^
BeginnerBasics
Assert the absolute beginning of the string or line (when multiline flag is enabled).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
$
BeginnerBasics
Assert the absolute end of the string or line (when multiline flag is enabled).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
\b
BeginnerBasics
Assert a word boundary (junction where letters and non-letters transition).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
\B
BeginnerBasics
Assert a non-word boundary.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)

Character Classes

.
BeginnerBasics
Match any single character except newline.

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

Console / Terminal
true
\d
BeginnerBasics
Match any numeric digit. Equivalent to character range [0-9].

When 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

Console / Terminal
true
\D
BeginnerBasics
Match any non-digit character. Equivalent to range [^0-9].

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 = /\D/;
const testStr = "test_data_2026";
const result = regex.test(testStr);

Output Example

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
\w
BeginnerBasics
Match any word character (alphanumeric characters plus underscore).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
\W
BeginnerBasics
Match any non-word character.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
\s
BeginnerBasics
Match any whitespace character (spaces, tabs, system line breaks).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
\S
BeginnerBasics
Match any character that is not whitespace.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
[a-z]
BeginnerBasics
Match any single lowercase alphabetical character within the specified set.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
[^A-Z]
BeginnerBasics
Negated character class: match any character except uppercase alphabetical letters.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)

Quantifiers

*
BeginnerBasics
Match 0 or more occurrences of the preceding token.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
+
BeginnerBasics
Match 1 or more occurrences of the preceding token.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
?
BeginnerBasics
Match 0 or 1 occurrence of the preceding token. Makes search optional.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
*?
BeginnerBasics
Lazy quantifier: match as few characters as possible (non-greedy).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
{n}
BeginnerBasics
Match exactly 'n' occurrences of the preceding token.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
{n,}
BeginnerBasics
Match at least 'n' or more occurrences of the preceding token.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
{n,m}
BeginnerBasics
Match between 'n' and 'm' occurrences of the preceding token.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)

Groups

(abc)
BeginnerBasics
Capturing group: group characters together to capture matching text for reuse.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
(?:abc)
BeginnerBasics
Non-capturing group: group characters without storing matching text in registry.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
(a|b)
BeginnerBasics
Match either pattern 'a' or pattern 'b' (alternation).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
\1
BeginnerBasics
Backreference to the exact text matched by capturing group number 1.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)

Assertions

(?=abc)
BeginnerBasics
Positive Lookahead: assert that the pattern 'abc' exists directly ahead.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
(?!abc)
BeginnerBasics
Negative Lookahead: assert that the pattern 'abc' does not exist directly ahead.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
(?<=abc)
BeginnerBasics
Positive Lookbehind: assert that the pattern 'abc' exists directly behind.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
(?<!abc)
BeginnerBasics
Negative Lookbehind: assert that the pattern 'abc' does not exist directly behind.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)

Flags

g
BeginnerBasics
Global flag: perform a global search, finding all matches rather than stopping at first.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
i
BeginnerBasics
Case-insensitive flag: ignore case variations (e.g. matching both a and A).

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
m
BeginnerBasics
Multiline flag: treat starting (^) and ending ($) anchors as matching lines instead of string boundaries.

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

Console / Terminal
Result: true (Pattern matches standard alphanumeric sample inputs)
s
BeginnerBasics
Dotall flag: allows dot (.) to match newline characters.

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

Console / Terminal
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

Error

Uncaught SyntaxError: Invalid regular expression: missing /

Solution

Check that your regex delimiters are properly balanced and that special chars like slashes are escaped (e.g. \/).

Error

Pattern matches more text than expected (Greedy Match)

Solution

Append a question mark (?) after your quantifier to make the search non-greedy (lazy) (e.g. changed /<.*>/ to /<.*?>/).

Error

Matches fail in multiline texts

Solution

Enable the multiline (m) flag so starting (^) and ending ($) anchors correspond to line breaks rather than string edges.

Error

Special character matching fails

Solution

Ensure you escape special regex meta-characters (like . * + ? ^ $ ( ) [ ] { } | \) with a backslash to match literal values.

Error

Search matches wrong cases

Solution

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.