Regex Tester & DebuggerSpecialized Version
🔍

Regex Matcher

Match regex

//g
Flags:
Examples:

Regex Matcher

Match text against regular expression patterns to find all occurrences. Test regex patterns and see highlighted matches in real-time.

Regex Matching Basics

ConceptSyntaxDescription
LiteralabcMatches exact characters
Any character.Matches any single character
Character class[abc]Matches a, b, or c
Negated class[^abc]Matches anything except a, b, c
Range[a-z]Matches lowercase letters
Digit\dMatches any digit (0-9)
Word character\wMatches [a-zA-Z0-9_]
Whitespace\sMatches space, tab, newline

Regex Matcher Implementation

``javascript function matchRegex(pattern, text, flags = 'g') { try { const regex = new RegExp(pattern, flags); const matches = []; let match;

while ((match = regex.exec(text)) !== null) { matches.push({ match: match[0], index: match.index, groups: match.slice(1), namedGroups: match.groups || {} });

// Prevent infinite loop for zero-length matches if (match.index === regex.lastIndex) { regex.lastIndex++; } }

return { pattern, flags, matchCount: matches.length, matches }; } catch (e) { return { error: e.message }; } }

// Example const result = matchRegex('\\d{3}-\\d{4}', 'Call 555-1234 or 555-5678'); // matchCount: 2 // matches: ['555-1234', '555-5678'] `

Regex Quantifiers

QuantifierMeaningExample
*0 or morea* → "", "a", "aaa"
+1 or morea+ → "a", "aaa"
?0 or 1a? → "", "a"
{n}Exactly na{3} → "aaa"
{n,}n or morea{2,} → "aa", "aaa"
{n,m}n to ma{2,4}` → "aa", "aaa", "aaaa"

Match Flags

FlagNameEffect
gGlobalFind all matches
iCase-insensitiveIgnore case
mMultiline^ and $ match line boundaries
sDotall. matches newlines

Frequently Asked Questions

What is the difference between * and + in regex?

* matches zero or more occurrences (optional), while + requires at least one occurrence. Pattern a* matches "", "a", "aa". Pattern a+ matches "a", "aa" but not empty string. Use * when the item is optional, + when it must appear at least once.

How do I match the literal dot character?

Escape it with a backslash: \\. Without escaping, . is a wildcard matching any character. To match "file.txt", use file\\.txt. In JavaScript strings, you need double backslash: "file\\\\.txt" because \\ itself needs escaping.

Why does my regex match more than expected?

Regex quantifiers are greedy by default—they match as much as possible. Pattern a.*b on "aXbYb" matches the entire string, not just "aXb". Add ? for lazy matching: a.*?b matches the shortest "aXb". Also check if you need anchors (^ and $).

Related Tools

Explore other tools you might find useful:

Related Calculators