Regex Field

Flags regexes that look right but match too much or hang: unescaped dots, missing anchors, greedy wildcards and catastrophic backtracking.

StopsRegex matches more than intended

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/regex-field.json

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

Usage

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 it 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

Props

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.

Accessibility

  • 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.

Limitations

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 it stops

Regex matches more than intended

Also known as

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

Field type

Dependencies

Pairs well with