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 |
|---|---|---|
| Literal | abc | Matches 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 | \d | Matches any digit (0-9) |
| Word character | \w | Matches [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 more | a* → "", "a", "aaa" |
+ | 1 or more | a+ → "a", "aaa" |
? | 0 or 1 | a? → "", "a" |
{n} | Exactly n | a{3} → "aaa" |
{n,} | n or more | a{2,} → "aa", "aaa" |
{n,m} | n to m | a{2,4}` → "aa", "aaa", "aaaa" |
Match Flags
| Flag | Name | Effect |
|---|---|---|
| g | Global | Find all matches |
| i | Case-insensitive | Ignore case |
| m | Multiline | ^ and $ match line boundaries |
| s | Dotall | . matches newlines |