Regex Tester

A pattern against a test string, with matches highlighted and capture groups listed. Zero-length matches are guarded — `/a*/g` matches empty at every position and hangs a naive exec loop.

src/components/ui/regex-tester.tsx

About

From the component's own source, where it was written.

A regular expression against a test string, with matches highlighted.

The pattern is compiled in a try/catch and the error is shown as you type. An unfinished pattern is the normal state while typing, so a syntax error is a hint, not a failure — the previous good match set is not thrown away.

Zero-length matches are guarded. /a*\/g matches empty at every position and a naive exec loop over it never terminates; the index is advanced manually when a match consumes nothing. This is the bug in most hand-rolled regex testers.

Named and numbered capture groups are listed per match. A pattern that matches but captures the wrong thing is the failure people actually hit, and a highlight alone cannot show it.

Install

npx astralyx-ui add regex-tester

Usage

import { RegexTester } from '@/components/ui/regex-tester'

<RegexTester pattern={pattern} onPatternChange={setPattern} />

Composer

Change the props and the source below follows.

ada@example.com, marc@northwind.com not-an-email devon@example.com
Matches3
  • 0ada@example.comGroups:1="ada"2="example"user="ada"
  • 17marc@northwind.comGroups:1="marc"2="northwind"user="marc"
  • 49devon@example.comGroups:1="devon"2="example"user="devon"
Props
tsx
<RegexTester pattern={'(?<user>\w+)@(\w+)\.com'} flags="g" />

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

RegexTester

Extends Omit<ComponentProps<'div'>, 'onChange' | 'children'>.

PropTypeDefaultDescription
patternstring—All three are controlled or uncontrolled independently, so you can pin the pattern and let the test string be edited.
onPatternChange(pattern: string) => void——
flagsstring—All three are controlled or uncontrolled independently, so you can pin the pattern and let the test string be edited.
onFlagsChange(flags: string) => void——
inputstring—All three are controlled or uncontrolled independently, so you can pin the pattern and let the test string be edited.
onInputChange(input: string) => void——
patternLabelReactNode'Pattern'—
flagsLabelReactNode'Flags'—
inputLabelReactNode'Test string'—
matchesLabelReactNode'Matches'—
noMatchLabelReactNode'No matches'—
groupsLabelReactNode'Groups'—

Types

RegexMatch

A regular expression against a test string, with matches highlighted.

FieldTypeDescription
indexnumber—
textstring—
groups{ name: string; value: string }[]Numbered and named. A pattern that matches but captures the wrong thing is the failure people actually hit, and a highlight cannot show it.

Behaviour

TopicDetailDescription
errorsshown, not thrownAn unfinished pattern is the normal state while typing, so a syntax error is a hint — the previous match set is not discarded.
zero-lengthguardedlastIndex is advanced manually — the bug in most hand-rolled regex testers.