This site is not affiliated with or endorsed by Vercel, Inc. Read the docs, then deploy or run each experiment yourself.
AI

Regex Explainer Lab

Explain a regex in plain English (AI) and highlight matches in a test string with pure logic.ts helpers.

Run this experiment yourself

Demos are not embedded on this site. Deploy a standalone copy on Vercel or run the experiment app locally.

Local development

cd apps/experiments/regex-explainer-lab
pnpm install
pnpm dev

Then open http://localhost:3010.

This is an experimental demo. Use it as a starting point for your own projects.

Regex Explainer Lab splits responsibilities: logic.ts compiles the pattern and highlights matches (no AI), while POST /api/explain uses generateObject for plain-English explanation and token meanings. The UI shows live highlights even before you call the API. No AI keys - 503 for explain only; highlights still work client-side.

Features

  • Local highlighting – highlightMatches + renderHighlightedHtml.
  • AI explanation – overview, parts, caveats.
  • Flags normalize – always includes g for multi-match UI.
  • Sample name regex – title-case phrases.
  • Honest unavailable state for AI.

Server Reference

POST /api/explain

{ "pattern": "\\d+", "flags": "g", "testString": "a12b" }

Success (200)

{
  "plainEnglish": "Matches one or more digits.",
  "parts": [{ "token": "\\d+", "meaning": "one or more digits" }],
  "caveats": ["Uses JavaScript RegExp semantics."],
  "highlights": {
    "ok": true,
    "flags": "g",
    "matches": [{ "start": 1, "end": 3, "text": "12", "index": 0 }]
  },
  "provider": "ai-gateway"
}
StatusCause
503No AI provider
400Invalid / empty pattern

Implementation Details

Highlight in logic.ts

highlightMatches caps matches at 50 and escapes HTML for safe dangerouslySetInnerHTML.

Explain with generateObject

Only runs after a successful local compile of the same pattern/flags.

Use Cases

  • Teach regex with live match feedback.
  • Document patterns in plain English.
  • Unit-test highlighting without mocks.

Limitations

  • JS RegExp only (not PCRE / Rust regex).
  • Catastrophic backtracking is possible with hostile patterns.
  • No auth / rate limits.

Use in your project

Import highlightMatches anywhere; keep AI explanation optional behind a Route Handler.

Deployment

Deploy on Vercel

Local Development

cd apps/experiments/regex-explainer-lab
pnpm install
pnpm dev

Configuration

VariableRequiredPurpose
AI_GATEWAY_API_KEYYes (or OPENAI_API_KEY)Preferred provider for explain
OPENAI_API_KEYNoFallback
AI_MODELNoModel override

Vercel / Next.js Features Used

  • AI SDK generateObject
  • Vercel AI Gateway
  • Route Handlers
  • Zod

Next Steps

On this page