test-regex
Test a regular expression pattern against any text to find matches, capture groups, and their positions. Validate regex quickly.
Instructions
Test a regular expression against text. Returns all matches with positions and capture groups.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | Regex pattern | |
| text | Yes | Text to test | |
| flags | No | Regex flags (default: g) | g |
Implementation Reference
- src/index.js:510-522 (handler)The handler function that executes the test-regex tool logic. Creates a RegExp from the pattern and flags, uses matchAll() to find all matches, and formats them with positions and capture groups.
function handleTestRegex(args) { const re = new RegExp(args.pattern, args.flags || "g"); const matches = [...args.text.matchAll(re)]; if (!matches.length) return "No matches found."; const lines = matches.map((m, i) => { let line = `Match ${i + 1}: "${m[0]}" at index ${m.index}`; if (m.length > 1) { line += "\n Groups: " + m.slice(1).map((g, j) => `$${j + 1}="${g}"`).join(", "); } return line; }); return `Found ${matches.length} match(es):\n\n${lines.join("\n")}`; } - src/index.js:184-200 (schema)Input schema definition for test-regex. Defines pattern (string), text (string), and optional flags (string, default 'g') as inputs.
{ name: "test-regex", description: "Test a regular expression against text. Returns all matches with positions and capture groups.", inputSchema: { type: "object", properties: { pattern: { type: "string", description: "Regex pattern" }, text: { type: "string", description: "Text to test" }, flags: { type: "string", description: "Regex flags (default: g)", default: "g", }, }, required: ["pattern", "text"], }, - src/index.js:830-843 (registration)Registration of the test-regex tool in the HANDLERS map, mapping 'test-regex' to the handleTestRegex function.
"test-regex": handleTestRegex, "build-cron": handleBuildCron, "convert-eth-units": handleConvertEthUnits, "validate-wallet": handleValidateWallet, "encode-url": handleEncodeUrl, "browse-underground": handleBrowse, "search-underground": handleSearch, "buy-from-underground": handleBuy, "get-free-content": handleGetFreeContent, "verify-receipt": handleVerifyReceipt, "agent-identity": handleAgentIdentity, "agent-mesh": handleAgentMesh, "pet-rock-lobster": handlePetRockLobster, };