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
| Concept | Syntax | Description |
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 | \s | Matches 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
| Quantifier | Meaning | Example |
*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