Regex Tester
Test JavaScript regular expressions against text or files with match and capture-group evidence, replacement previews, and timeout-protected runs.{{ summaryTitle }}
{{ summaryLine }}
- {{ warning }}
| # | Match | Span | Line:column | Captures | Copy |
|---|---|---|---|---|---|
| No matches for the current pattern and tested sample. | |||||
| {{ row.number }} | {{ row.match }} | {{ row.span }} | {{ row.position }} | {{ row.captures }} | |
The chart renderer is unavailable. Match line evidence remains available in the ledgers.
{{ replacementText }}A regular expression can find a useful text shape and still be wrong for the job. A pattern that catches one valid identifier may also match a substring inside an invalid one. A pattern that works in a small sample may behave differently with another regex dialect, different flags, a much larger input, or a replacement string that interprets capture tokens.
JavaScript regular expressions operate on strings and return matches with positions and optional capture groups. Anchors restrict where a match may begin or end, character classes describe allowed characters, quantifiers control repetition, and groups preserve parts of the match for inspection or replacement. Flags then change the meaning of the same pattern by enabling repeated matching, case folding, multiline anchors, dot-all behavior, Unicode handling, indices, or sticky matching.
| Test material | What it reveals |
|---|---|
| Expected matches | Whether the intended text is accepted and the right part is captured |
| Near misses | Whether missing anchors, loose classes, or optional groups admit bad text |
| Empty, multiline, and Unicode cases | Whether boundaries and flags behave as intended outside the simplest sample |
| Long adversarial input | Whether nested alternatives or quantifiers create unacceptable execution time |
Match success is not the same as validation. Searching for a date-shaped substring is different from requiring the entire field to be a real calendar date. Likewise, a replacement preview proves what the current JavaScript runtime produced for the tested text; it does not prove that another language, command-line utility, database, or production runtime uses identical syntax.
Performance needs its own check. Some patterns can take rapidly increasing time on carefully chosen input. A browser timeout limits one local test, but a quick result on one sample does not establish that the expression is safe for untrusted or much larger production data.
How to Use This Tool:
Build a small sample with both intended matches and realistic failures, then test the exact JavaScript pattern, flags, and replacement you plan to use.
- Paste text into Sample text or load one text-like file no larger than 4 MiB. Include near misses instead of testing only known-good lines.
- Enter the Pattern without slash delimiters, then select the JavaScript Flags. Use global or sticky when repeated non-overlapping matches are required.
- Add a Replacement only when substitution matters. An empty replacement previews removal; JavaScript replacement tokens such as
$&,$1,$<name>, and$$keep their normal meanings. - Set the Match cap, Execution timeout, and Sample limit to keep evidence reviewable. If execution stops, reduce the sample or simplify the pattern before considering a larger timeout.
- Review the highlighted spans, match positions, captures, warnings, and replacement text. Treat truncation or cap warnings as incomplete coverage rather than a passing test.
Interpreting Results:
A matched result means at least one match occurred in the tested prefix. No match means none occurred there. Neither result says anything about characters beyond a reported sample limit.
- Span gives the start and end offsets, while Line:column locates the match in the tested text. JavaScript string offsets count UTF-16 code units, so they may differ from user-perceived character counts.
- Without
gory, JavaScript returns only the first match. Withoutd, capture values remain available but capture start and end indices are not. - Replacement text is the result of applying the same pattern and flags to the tested sample. Inspect it directly, especially when optional or named groups can be absent.
- A timeout is a warning about this pattern and sample. A fast completion is not a security guarantee; repeat tests with realistic maximum sizes and hostile near-matches.
Technical Details:
The governing mechanism is a bounded JavaScript string transformation, not an arithmetic calculation. Pattern compilation, repeated matching, capture extraction, position mapping, and replacement all use the current browser's ECMAScript behavior.
Transformation Core:
- Whitespace is removed from the flag list, duplicate flags are collapsed, and flags are ordered as
d g i m s u v y. Theuandvflags cannot be combined. - Only the leading Sample limit characters are tested. A warning records when the original input was longer.
- The pattern is compiled in an isolated browser task. Global or sticky expressions advance through non-overlapping matches; other expressions return at most the first match.
- Each match is projected into its text, span, line and column, capture values, and optional capture indices. Zero-width matches advance by one code unit so repeated matching cannot stay at the same position.
- A second expression with the same pattern and flags produces the replacement preview. The result is returned only if the isolated task completes before the timeout.
Rule Core:
| Flag | Meaning in this test | Important interaction |
|---|---|---|
d | Expose match and capture indices | Needed for capture spans |
g | Find repeated non-overlapping matches | Uses and advances the expression position |
i | Ignore case according to JavaScript rules | Unicode behavior can depend on the Unicode mode |
m | Let line boundaries affect anchors | ^ and $ can apply per line |
s | Let dot match line terminators | Can broaden a pattern across lines |
u | Use Unicode-aware pattern semantics | Mutually exclusive with v |
v | Use Unicode sets mode | Mutually exclusive with u and runtime support varies |
y | Match only at the current expression position | Repeated sticky matching stops at the first gap |
| Boundary | Allowed range | Behavior at the limit |
|---|---|---|
| Pattern length | 1 to 10,000 characters | Empty or longer patterns are rejected |
| Loaded sample file | Up to 4 MiB | Larger files are not read |
| Tested sample prefix | 1,000 to 500,000 characters | Later text is omitted and truncation is reported |
| Listed matches | 1 to 2,000 | Matching stops at the cap and a warning is added |
| Execution timeout | 50 to 5,000 ms | The isolated task is terminated when time expires |
| Replacement length | Up to 100,000 characters | Longer replacement input is rejected |
The line chart displays at most the 24 lines with the most matches when more than 24 matched lines exist. The match ledger still keeps every listed match up to the selected cap.
Privacy and Accuracy Notes:
Pasted text and loaded files are read in the browser. Pattern execution takes place in an isolated local browser task that can be terminated at the selected timeout. Do not assume that local testing makes sensitive test data safe to copy into later reports or exports.
Results describe the current browser's JavaScript dialect. Port the test cases, not just the pattern, when moving to grep, PCRE, Python, .NET, a database engine, or another JavaScript version.
Worked Examples:
Anchoring a ticket code
Against lines containing OPS-2026 ready and OPS-20260 extra digit, the pattern OPS-[0-9]{4} matches both. Requiring a line start and a following space, such as ^OPS-[0-9]{4}[ ] with multiline mode, rejects the extra digit in this fixture.
Inspecting a named capture replacement
With a named group for a capitalized word and the indices flag enabled, the ledger can show the whole-match span and the capture span. A replacement such as [$&] then proves exactly which text is wrapped without changing unmatched text.
References:
- ECMAScript Language Specification: RegExp Objects, Ecma International.
- Regular expression Denial of Service, OWASP Foundation.
- How to test a regular expression with grep, Simplified Guide.