Build and test regular expressions
✓Works with OpenClaudeYou are a regex expert. The user wants to build, test, and validate regular expressions with immediate feedback.
What to check first
- Open a terminal or Node.js REPL to test regex patterns in real time
- Have sample input data ready that matches and doesn't match your intended pattern
- Identify the regex flavor you're targeting (JavaScript, Python, PCRE, etc.) — JavaScript is assumed here
Steps
- Define what you want to match: email addresses, phone numbers, URLs, specific formats, etc.
- Start with a simple pattern and test it against sample strings using
.test()or.exec() - Use character classes
[a-z], quantifiers+,*,?,{n,m}, and anchors^,$to refine the pattern - Add capturing groups
()if you need to extract parts of the matched string - Test edge cases: empty strings, special characters, Unicode, boundaries with
\b,\B - Use alternation
|for multiple acceptable patterns (email domains, file extensions) - Apply flags:
gfor global,ifor case-insensitive,mfor multiline,sfor dotall - Escape special characters with
\when matching literal dots, parentheses, brackets, etc.
Code
// Regex Builder & Tester
class RegexBuilder {
constructor() {
this.pattern = '';
this.flags = '';
this.testResults = [];
}
// Set the pattern and flags
setPattern(pattern, flags = '') {
this.pattern = pattern;
this.flags = flags;
return this;
}
// Test single string against pattern
test(input) {
try {
const regex = new RegExp(this.pattern, this.flags);
const result = regex.test(input);
this.testResults.push({ input, result, match: null });
return result;
} catch (e) {
console.error('Invalid regex:', e.message);
return false;
}
}
// Extract matches with details
exec(input) {
try {
const regex = new RegExp(this.pattern, this.flags);
let match;
const matches = [];
if (this.flags.includes('g')) {
while ((match = regex.exec(input)) !== null) {
matches.push({
full: match[0],
groups: match.slice(1),
index: match.index
});
}
} else {
match = regex.exec(input);
if (match) {
matches.push({
full: match[0],
groups: match.slice(1),
index: match.index
});
}
}
return matches;
} catch (e) {
console.error('Regex error
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Code Generation Skills
Other Claude Code skills in the same category — free to download.
Type Generator
Generate TypeScript types from JSON/API responses
Interface from JSON
Generate interfaces from JSON samples
Enum Generator
Generate enums from constant values
Boilerplate Reducer
Generate boilerplate code patterns
SQL Generator
Generate SQL queries from natural language
Mock Data Generator
Generate realistic mock data (Faker.js)
Type Guard Generator
Generate TypeScript type guards
Regex Lookahead
Write regex with lookaheads, lookbehinds, and named groups
Want a Code Generation skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.