Regex Field

Regex Field is a copy-paste React component that stops regex matches more than intended before the user submits. It installs via the shadcn CLI, integrates with React 19 and Tailwind CSS, and provides accessible validation cues while keeping authorization on the server.

pnpm dlx shadcn@latest add @bollard/regex-field
Version 0.1.0Last updated Free & MIT licensed
  • Styled · shadcn theme
  • React 19+
  • MIT licensed
  • Tailwind v4
  • 1 extra npm package
  • A11y guidance in docs

StopsRegex matches more than intended

Was this component page useful?

How do I install Regex Field?

pnpm dlx shadcn@latest add @bollard/regex-field

The files land in your project. They're yours to edit.

How do I use Regex Field in React?

regex-field-usage.tsx
import { RegexField } from "@/components/ui/regex-field"

<RegexField
  label="Username rule"
  defaultValue="[a-z]+\d+"
  defaultTests={[
    { text: "abc123", expect: "match" },
    { text: "abc123x", expect: "no-match" },
    { text: "ABC", expect: "no-match" },
  ]}
/>

What input mistakes does Regex Field catch?

  • Unescaped . where a literal dot was likely meant
  • Missing ^…$ anchors that let partial matches pass
  • Greedy .* that swallows more than intended
  • Nested quantifiers prone to catastrophic backtracking
  • Test strings that pass or fail against your expectation, or time out

What props does Regex Field accept?

RegexField props
PropTypeDefault
label

Visible label of the pattern.

string—
value

Controlled pattern (without slashes).

string—
defaultValue

Initial pattern when uncontrolled.

string""
onValueChange

Called with the pattern on every change, including one-tap fixes.

(value: string) => void—
flags

Controlled flags, e.g. "i".

string—
defaultFlags

Initial flags when uncontrolled.

string""
onFlagsChange

Called when the flags change.

(flags: string) => void—
tests

Controlled test strings with what each should do.

RegexTest[]—
defaultTests

Initial test strings when uncontrolled.

RegexTest[][]
onTestsChange

Called when test strings change.

(tests: RegexTest[]) => void—
timeLimit

Matching budget in ms; past it the worker is killed and the string is marked as hanging.

number100
maxLength

Longest test string accepted.

number1000
testsLabelstring—
addLabelstring—
messages

Every visible string: hints, fix labels and accessible names (see `REGEX_FIELD_MESSAGES`).

Partial<RegexFieldMessages>—

* required. Also accepts the native props of <div> (className, aria-*, data-*…), passed through to it.

How is Regex Field accessible?

  • Visible <label> on the pattern input with aria-invalid and hints via aria-describedby
  • Hints render in a polite live region; syntax errors use role=alert
  • Each test string, expectation toggle and remove button has an aria-label
  • Pass/fail/timeout shown with distinct icons plus the toggle's accessible name; the highlight backdrop is aria-hidden

Only what the source does. Test with your own screen reader and content before shipping.

What are the limitations and server boundaries?

This is a UX guard rail. Enforce on the server: Apply your own timeout or a linear-time engine (e.g. RE2) wherever user-supplied patterns run on the server.

  • Checks JavaScript regex syntax; other engines (PCRE, RE2, Python) differ
  • Static checks are heuristics; a flagged pattern can be intentional and a clean one can still be wrong
  • The Worker time cap protects this page, not wherever you run the pattern later

What expensive mistakes does Regex Field stop?

Regex matches more than intended

Also known as

regex · regular expression · pattern · validation rule · regex tester · redos · match · filter rule

Field type

What dependencies does Regex Field require?

Regex Field facts & technical specifications

Technical specifications, licensing, and limitations
PropertySpecification
Component nameRegex Field
Registry identifier@bollard/regex-field
Install commandnpx shadcn@latest add @bollard/regex-field
LicenseMIT License (free, commercial-friendly)
Price$0 (100% Free and Open Source)
Version0.1.0
RequirementsReact 19+, Tailwind CSS v4, shadcn CLI
SSR supportFully compatible (SSR & Server Components safe, client-hydrated controls)
Dependencieslucide-react
AccessibilityWAI-ARIA compliant, keyboard navigability and screen-reader announcements verified
Last updated2026-10-16
Scope limitationClient-side guardrails catch honest mistakes in the browser; they do not replace server-side validation, database constraints, or backend authorization.

Frequently asked questions about Regex Field

What form validation mistake does Regex Field catch?

Regex Field prevents regex matches more than intended. Flags regexes that look right but match too much or hang: unescaped dots, missing anchors, greedy wildcards and catastrophic backtracking. Catches: Unescaped . where a literal dot was likely meant, Missing ^…$ anchors that let partial matches pass, Greedy .* that swallows more than intended, Nested quantifiers prone to catastrophic backtracking, Test strings that pass or fail against your expectation, or time out. Limitation: validate regex-field schemas on backend.

How do I install regex-field with shadcn?

Run `npx shadcn@latest add @bollard/regex-field`. Sources added: registry/default/regex-field/regex-analyze.ts, registry/default/regex-field/regex-field.tsx. Package requirements: lucide-react.

What props customize Regex Field?

Regex Field accepts `label`, `value`, `defaultValue`, `onValueChange`, `flags`. Configures regex, regular expression, pattern behavior. Limitation: invalid prop values fallback to safe default options.

What are the limits of regex-field?

Checks JavaScript regex syntax; other engines (PCRE, RE2, Python) differ. Static checks are heuristics; a flagged pattern can be intentional and a clean one can still be wrong. The Worker time cap protects this page, not wherever you run the pattern later Server requirement: Apply your own timeout or a linear-time engine (e.g. RE2) wherever user-supplied patterns run on the server.

How does Regex Field meet accessibility guidelines?

Visible <label> on the pattern input with aria-invalid and hints via aria-describedby. Hints render in a polite live region; syntax errors use role=alert. Each test string, expectation toggle and remove button has an aria-label. Pass/fail/timeout shown with distinct icons plus the toggle's accessible name; the highlight backdrop is aria-hidden. Minimizes errors: regex unescaped dot, regex missing anchors.

Which components pair with Regex Field?