Regex Sample Matches Checker
Test a JavaScript regex against examples that should pass or fail, with whole-line matching, flag checks, match spans, and timeout guards.| Set | Line | Sample | Expected | Actual | Verdict | Match span | Copy |
|---|---|---|---|---|---|---|---|
| No sample rows are ready. | |||||||
| {{ row.set }} | {{ row.line }} | {{ row.sample }} | {{ row.expected }} | {{ row.actual }} | {{ row.verdict }} | {{ row.matchSpan }} | |
| Signal | Status | Detail | Copy |
|---|---|---|---|
| {{ row.signal }} | {{ row.status }} | {{ row.detail }} |
A regular expression rarely fails only by refusing to compile. The riskier failure is a pattern that accepts a value with extra characters, misses a valid value after a small format change, or finds a match in the middle of a string that was supposed to be checked as a complete value. That matters for identifiers, log filters, filenames, form validation, migration rules, and any review where one match changes what gets routed, accepted, or rejected.
A sample contract makes the intended boundary visible. Positive examples show the shapes that must be accepted. Negative examples show nearby shapes that must stay out. The negative set often gives the strongest evidence because it catches overmatching: lowercase variants, extra suffixes, missing separators, unexpected whitespace, doubled tokens, and values that look valid at a glance.
- Pattern
- The text rule itself, such as a literal prefix, a digit count, a character class, or a group of alternatives.
- Flags
- Options that change how the pattern reads the sample, including case, multiline anchors, dot behavior, Unicode mode, global evidence, indices, and sticky matching.
- Anchors
- Boundary assertions such as
^and$that decide whether a substring can pass or the whole value must satisfy the pattern. - Near miss
- A sample that is intentionally close to valid but should fail, such as
OPS-20260when exactly four digits are required.
Substring matching is the common trap. A pattern for [A-Z]{3}-\d{4} can find OPS-2026 inside OPS-20260, which is useful for search but wrong for a complete asset code. Whole-value validation needs anchors or an equivalent full-sample rule so leftover characters cannot hide after a valid-looking token.
JavaScript regular expressions have their own syntax and flag behavior. A pattern copied from another language may need escaping changes, and Unicode-aware matching can change how characters and sets are interpreted. Case-insensitive matching, multiline anchors, and dot-all behavior are especially easy to leave enabled after a test, which can make the sample contract pass for the wrong reason.
Sample testing does not prove a regex is correct for every possible string. It shows how the current examples behave under the selected rule and flags. Performance also belongs in the review because repeated groups, nested quantifiers, and overlapping alternatives can backtrack heavily on crafted nonmatches. A pattern that times out on a small fixture deserves a simpler design before it handles untrusted text.
How to Use This Tool:
Use the checker as a small regression suite for one JavaScript regular expression and one fixture set at a time.
- Enter the expression in Pattern without surrounding slash characters. The page reports a pattern error if the text or selected flags cannot compile.
- Select only the Flags needed for the case. The supported set is
d,g,i,m,s,u,v, andy;uandvare mutually exclusive. - Choose Any regex hit passes for search behavior, or Whole line must match when each sample is one complete value.
- Set Line handling before pasting fixtures. Trim mode cleans copied lists; preserve mode keeps leading and trailing spaces as part of the sample. Blank lines are ignored.
- Paste accepted examples in Should match and rejected examples in Should not match, one nonblank sample per line.
- Use Load sample, Swap sets, or Reset samples when you need a clean fixture, an inverted test, or empty sample lists without changing the pattern.
- Open Advanced when a large or risky suite needs bounds. The row cap, execution timeout, sample text limit, and per-sample match cap keep the browser check controlled.
Start with Failure Brief when the summary says the contract needs review, then inspect Sample Verdicts for the exact row and match span. Use Sample Outcome Mix for a quick pass/fail distribution and JSON when you need a portable result record.
Interpreting Results:
All samples pass means every positive row matched and every negative row stayed rejected. Add samples means the pattern has no useful evidence yet. Check pattern or Fix pattern means the expression failed to compile, timed out, missed an expected match, matched a reject row, or ran into a guardrail.
| Result cue | Meaning | Useful follow-up |
|---|---|---|
| Pass | The row behaved as expected under the current pattern, flags, line handling, and match rule. | Keep the row as a regression example when changing the pattern later. |
| Missed | A Should match row did not match. | Loosen the specific class, quantifier, separator, anchor, or flag only enough to accept that valid case. |
| Overmatched | A Should not match row matched unexpectedly. | Check whether a valid substring is being found inside a bad sample, then use whole-line mode, anchors, or narrower alternatives. |
| Match span | The first stored match start and end index, or none. | A short span inside a longer sample usually points to substring overmatching. |
| Capped or timeout note | The run skipped extra rows, capped stored evidence, rejected oversized text, or stopped at the timeout. | Reduce the fixture size, simplify the expression, or raise limits only for trusted patterns and controlled samples. |
The Failure Brief separates pattern compilation, expected matches, reject-set behavior, sample handling, and guard status. That split helps avoid the wrong fix. A missed positive row calls for broader acceptance. An overmatched reject row calls for tighter boundaries or a less permissive flag.
Do not compare two revisions unless the settings match. Changing from any-hit matching to whole-line matching, trimming whitespace, or enabling case-insensitive mode can change the verdict even when the pattern text stays the same.
Technical Details:
JavaScript compiles a regular expression from pattern text plus a flag string. The pattern contributes tokens such as literals, character classes, groups, assertions, alternatives, and quantifiers. Flags change the matching environment: they can collect repeated evidence, ignore case, treat anchors as line-aware, make dot match line breaks, enable Unicode-aware parsing, request match indices, or force sticky searches.
The main contract is simple. A positive sample passes when the selected rule finds at least one match. A negative sample passes when the selected rule finds no match. In whole-line mode, the expression is evaluated as a complete-sample rule, so leftover text before or after the matched token fails the row.
Positive row passes: match exists
Negative row passes: no match exists
Whole-line mode: the full sample must satisfy the pattern
Any-hit mode: a matching substring is enough
Flag Map:
| Flag | Meaning | Common sample-test effect |
|---|---|---|
d |
Indices | Adds index detail where the browser supports match indices. |
g |
Global | Collects repeated matches up to the per-sample evidence cap. |
i |
Ignore case | Can make lowercase near misses pass when uppercase is part of the rule. |
m |
Multiline anchors | Makes ^ and $ apply to line boundaries inside a sample string. |
s |
Dot all | Allows . to match line breaks. |
u |
Unicode mode | Reads the pattern as Unicode code points and changes escape validity. |
v |
Unicode sets mode | Enables newer Unicode set behavior where supported; it cannot be combined with u. |
y |
Sticky | Matches from the current scan position, which matters when repeated evidence is collected. |
Sample Handling Rules:
| Area | Behavior | Why it matters |
|---|---|---|
| Pattern input | Uses JavaScript regular expression syntax without slash delimiters. | Typing /abc/i as the pattern is different from entering abc and choosing i. |
| Flag normalization | Duplicate flags are removed and flags are ordered consistently. | Equivalent flag selections compare cleanly across pattern revisions. |
| Blank lines | Blank sample lines are ignored. | Use a visible placeholder in your own notes if an empty string is an important case. |
| Trim mode | Leading and trailing whitespace is removed before testing each nonblank row. | Useful for pasted lists where line-edge spaces are not part of the value. |
| Preserve mode | Line-edge whitespace stays in the sample. | Use it when spaces, tabs, or padding are part of the regex requirement. |
| Zero-width matches | A zero-length match is reported as a zero-width span. | Repeated evidence still advances so a zero-width expression does not trap the result collection. |
Guardrails:
| Guardrail | Minimum | Maximum | Effect |
|---|---|---|---|
| Sample row cap | 1 | 1,000 | Limits the combined positive and negative rows checked in one run. |
| Execution timeout | 50 ms | 5,000 ms | Stops a browser run that does not finish inside the selected limit. |
| Sample text limit | 1,000 chars | 500,000 chars | Rejects oversized sample text before evaluation. |
| Per-sample match cap | 1 | 100 | Limits stored evidence for broad global or sticky expressions. |
Privacy and Safety Notes:
The pattern and samples are checked in your browser. That keeps the regex test itself local, but the text can still be exposed through normal browser surfaces such as history, copied output, downloaded reports, shared URLs, or extensions. Do not paste secrets, private tokens, production logs, or customer data unless that is acceptable for your environment.
A passing sample suite is not a security audit. It does not prove that the regex is safe against every input length, every runtime, or every engine. Treat timeout warnings and slow nonmatches as a reason to simplify the expression before using it with untrusted text.
Worked Examples:
Asset Code Validator
Pattern ^[A-Z]{3}-\d{4}$, no flags, and Whole line must match accepts ABC-1234, OPS-2026, and NET-0007. Reject rows such as abc-1234, ABCD-123, ABC-12X4, and OPS-20260 stay rejected. The brief shows the positive and negative sets passing.
Missing Anchor Overmatch
Pattern [A-Z]{3}-\d{4} can find OPS-2026 inside OPS-20260 when Any regex hit passes is selected. The reject row becomes Overmatched, and the match span points to the matched substring. Whole-line mode or explicit anchors corrects the boundary.
Case Flag Surprise
Pattern ^[A-Z]{3}-\d{4}$ with the i flag makes abc-1234 match even when uppercase letters are required. Removing the flag is better than changing the character class when uppercase is part of the contract.
Whitespace-Sensitive Input
If a value with a leading space should fail, preserve line text before testing it. Trim mode would remove the line-edge space and could hide the failure. Preserve mode keeps the row closer to the exact input that the regex will see.
Timeout During Review
A pattern with nested repetition can hit the selected Execution timeout on a long nonmatching sample. The right fix is usually to simplify the repeated structure or narrow the input rule, not just to raise the timeout.
FAQ:
Should I use whole-line matching?
Use whole-line matching when each row is one complete value, such as an ID, code, slug, username, or filename. Use any-hit matching when you are intentionally searching inside longer text.
Why did a reject sample match?
Check the match span first. If only part of the row matched, switch to whole-line mode or add anchors. If a case variant matched, review the i flag. If a newline changed the result, review m and s.
Why are blank lines ignored?
The checker treats nonblank lines as sample rows. If the empty string is part of your validation rule, keep a separate note for that case and test the pattern in the exact runtime where it will be used.
Why did a pattern that works elsewhere fail here?
Regex syntax is not identical across languages. This checker uses JavaScript regular expression behavior, so features, escaping rules, Unicode modes, and flags from another runtime may need adjustment.
Does a pass mean the regex is safe for production?
No. A pass means the listed samples satisfy the current contract. Add real boundary cases, test in the runtime where the regex will run, and simplify patterns that time out or backtrack heavily.