Regex Sample Matches Checker
Check JavaScript regex patterns against positive and negative sample lines, flags, whole-line matching, verdicts, failure briefs, and timeout guards before release.{{ summaryTitle }}
| 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 }} |
Introduction:
A regular expression, or regex, is a compact rule for deciding whether text fits a pattern. It can accept a valid identifier, reject a malformed value, find a token inside a longer string, or confirm that an entire line has the expected shape. Small regex changes can have large effects because anchors, flags, character classes, and quantifiers all change which text is accepted.
Sample testing is the practical check that keeps a regex honest before it is copied into validation code, routing rules, log parsing, or data cleanup. A useful sample set includes the values that must pass and the near misses that must fail, such as lowercase variants, extra characters, missing separators, and partial strings. The negative examples matter as much as the positive ones because overmatching is often harder to spot than a missed match.
A passing sample contract does not prove that a regex is complete. It proves only that the chosen examples behave as expected under the selected flags and matching rule. Treat the result as a focused regression check: strong enough to catch known mistakes, but still dependent on the quality of the examples and the runtime where the regex will later run.
Performance also belongs in the review. Some regex structures can backtrack heavily on crafted nonmatching text, especially when repeated groups and overlapping alternatives interact. A timeout during sample testing is a reason to simplify the pattern or narrow the accepted input before the expression reaches a production path.
Technical Details:
JavaScript regular expressions are compiled from a pattern string and optional flags. The pattern describes the text shape, while the flags change matching rules such as case sensitivity, anchor behavior, Unicode handling, and repeated match collection. A boolean sample check is mainly asking whether at least one match exists for a given sample string.
The same pattern can mean different things depending on how much of the sample must be consumed. A substring check can pass when a valid-looking token appears inside a longer value. A whole-line check is stricter because the evaluated expression is wrapped so the sample must match from start to end.
Rule Core:
| Rule | Behavior | Why it matters |
|---|---|---|
| Pattern text | Entered as JavaScript RegExp syntax without surrounding slash delimiters. | Escapes and groups should be written for the JavaScript engine, not for a different regex dialect. |
| Flags | d, g, i, m, s, u, v, and y are normalized in JavaScript flag order when supported. |
Duplicate flags are removed, unsupported flags fail the run, and u cannot be combined with v. |
| Any regex hit | The submitted pattern is tested against each sample as a possible substring match. | Good for search patterns and token discovery, but it can hide trailing or leading junk. |
| Whole line | The effective pattern becomes ^(?:pattern)$. |
Best for validators where each sample line represents one complete value. |
| Positive samples | Rows in the should-match set pass only when the regex finds a match. | A missed positive row means the regex is too narrow for at least one accepted example. |
| Negative samples | Rows in the should-reject set pass only when the regex finds no match. | An overmatched negative row means the regex accepts a value that should remain invalid. |
Each nonblank sample row is evaluated independently. Trim mode removes leading and trailing spaces before testing, while preserve mode keeps those spaces as part of the sample. Global and sticky expressions collect match evidence with repeated execution; zero-width matches advance safely so a pattern that matches an empty position does not trap the evidence loop.
Guardrails And Limits:
| Control | 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 the worker if the browser does not finish the regex suite in time. |
| Sample text limit | 1,000 chars | 500,000 chars | Rejects oversized sample suites before evaluation. |
| Per-sample match cap | 1 | 100 | Limits stored match evidence for broad global or sticky expressions. |
The timeout guard is a safety signal, not a formal proof about future runtime. A regex that passes a small fixture set can still behave poorly on longer or adversarial input. Repeated groups, nested quantifiers, and overlapping alternatives deserve extra review before they are used on untrusted text.
Everyday Use & Decision Guide:
Start with the final shape of a real value. For an asset code such as ABC-1234, put accepted examples in Should match and obvious near misses in Should not match: lowercase, too many letters, too few digits, and a valid token with an extra trailing character. Choose Whole line must match when each row should be a complete value.
Keep the flag set small while debugging. The i flag can make lowercase rejects pass unexpectedly, and g or y changes how match evidence is gathered. If a browser does not support a selected flag, the run fails during pattern compilation instead of silently changing the rule.
- Use Trim line edges for copied fixture lists where accidental spaces should not count.
- Use Preserve exact line text when leading or trailing spaces are part of the value being tested.
- Check the Failure Brief first when the summary says fix pattern.
- Open Sample Verdicts to find the exact line that was missed or overmatched.
- Raise the timeout only for trusted patterns and controlled fixtures; a repeated timeout usually means the pattern needs simplification.
A pass is strongest when both sample sets are varied. Add boundary examples before relying on the result: shortest valid value, longest expected value, wrong separator, mixed case, extra suffix, and an empty-looking row if whitespace is meaningful.
Step-by-Step Guide:
Use the checker as a small regression suite for a JavaScript regex.
- Enter the regex in Pattern without leading and trailing slash characters. If the pattern cannot compile, the alert explains the pattern or flag problem.
- Select only the flags needed for the test. The flag badge in the summary shows the normalized flag string, or no flags when none are selected.
- Choose Any regex hit passes for search behavior, or Whole line must match when each row should be fully consumed.
- Set Line handling to trim copied lists or preserve exact whitespace.
- Paste one accepted value per line in Should match and one rejected value per line in Should not match.
- Use Sample row cap, Execution timeout, Sample text limit, and Per-sample match cap when a large suite or broad pattern needs bounds.
- Read the summary, then inspect Sample Verdicts and Failure Brief. If rows are skipped, blank lines are ignored, or match evidence is capped, the brief names the condition.
When the summary shows All samples pass, keep the same samples as a regression set and add new negative examples whenever a bug report or production edge case appears.
Interpreting Results:
The summary tells you whether the current sample contract is usable. All samples pass means every positive row matched and every negative row stayed rejected. Add samples means there is no evidence yet. Check pattern means the regex could not run because of a compile error, invalid flag choice, oversized sample text, missing worker support, or timeout.
| Output cue | Meaning | Follow-up |
|---|---|---|
| Missed | A should-match row did not match. | Loosen the class, quantifier, separator, or flag only enough to accept that valid example. |
| Overmatched | A should-reject row matched unexpectedly. | Add anchors, choose whole-line mode, narrow an alternation, or remove a permissive flag. |
| Match span | The start and end indexes of the first stored match, or none. | A short span inside a longer sample often points to substring matching when full validation was intended. |
| Worker guard | The suite finished within the selected timeout, or evidence was capped. | Treat timeouts and capped evidence as reasons to reduce fixture size or simplify the expression. |
Do not read a pass as broad correctness. It is a statement about the current rows, flags, line handling, and match rule. Reuse those settings when comparing revisions so a later result is testing the same contract.
Worked Examples:
Asset Code Validator
With pattern ^[A-Z]{3}-\d{4}$, no flags, and whole-line matching, samples such as ABC-1234 and OPS-2026 pass in Should match. Reject rows such as abc-1234, ABCD-123, and OPS-20260 stay rejected. The summary reads All samples pass, and the Failure Brief shows Pattern compile, Expected matches, and Reject set as pass signals.
Missing Anchor Overmatch
Pattern [A-Z]{3}-\d{4} can find OPS-2026 inside OPS-20260 when Any regex hit passes is selected. The negative row then appears as Overmatched with a Match span of 0-8. Switching to Whole line must match or adding explicit anchors makes the extra trailing digit fail as intended.
Case Flag Surprise
Using ^[A-Z]{3}-\d{4}$ with the i flag makes abc-1234 match even though it sits in Should not match. The summary changes to fix pattern, and Reject set reports a failure. Removing i is the better fix when uppercase is part of the rule.
Timeout During Review
A pattern with nested repetition can stop at the selected timeout when tested against a long nonmatching row. The alert says execution stopped after the guard interval and suggests reducing samples, simplifying the pattern, or raising the timeout for trusted inputs. For untrusted production text, simplification is usually safer than only increasing the guard.
FAQ:
Should I use whole-line matching?
Use whole-line matching when each sample row represents one complete value, such as an ID, code, slug, or filename. Use any-hit matching when you are deliberately searching for a token inside longer text.
Why did a reject row match?
A reject row becomes Overmatched when the regex finds any acceptable span in that sample. Check Match span, remove permissive flags such as i when case matters, or choose whole-line mode if partial matches are not allowed.
Why are some pasted lines missing?
Blank lines are ignored. If the suite exceeds Sample row cap, extra rows are skipped and the Failure Brief reports how many rows were capped.
What does a timeout mean?
A timeout means the worker did not finish before the selected millisecond guard. Reduce the sample set, simplify nested or overlapping regex structure, or raise the timeout only for trusted fixtures.
Are patterns and samples sent to a server?
The regex suite is evaluated in the browser through a timeout-guarded worker. The checker does not send the pattern or sample rows to a server for evaluation.
Glossary:
- Regex
- A regular expression pattern used to match text.
- Flag
- A JavaScript RegExp option such as
i,g, oruthat changes matching behavior. - Whole-line match
- A stricter check that requires the entire sample row to match the pattern.
- Positive sample
- A should-match row that is expected to be accepted by the regex.
- Reject sample
- A should-reject row that should not match the regex.
- Overmatch
- A failure where a reject sample matches unexpectedly.
- Zero-width match
- A match that succeeds without consuming characters, such as an anchor or boundary assertion.
- ReDoS
- Regular expression denial of service, where a slow regex can consume excessive runtime on crafted input.
References:
- Regular expressions - JavaScript, MDN Web Docs, last modified October 30, 2025.
- RegExp.prototype.test(), MDN Web Docs, last modified July 10, 2025.
- Regular expression Denial of Service - ReDoS, OWASP Foundation.