# Build, test and debug regex with capture groups in real time

> Online JavaScript regex tester free with live highlight, capture groups, gimsuy flags. Pattern library: email, URL, IP, phone, dates, hex color, slug, ISO.

Free in-browser tool (no upload): https://www.mauriziofonte.it/tools/regex-tester

Tester for **JavaScript regex**: paste a pattern (with `gimsuy` flags) and a test string, get **match highlight**, **capture groups**, **match count**. Categorized pattern library with ready-made regexes for: email, URL, IPv4/IPv6, MAC address, phone, ISO date, slug, hex color, plus Italian-specific (CF, P.IVA, IBAN). Fully.

## How to use the regex tester

1. **Enter pattern and flags** - Pattern: without delimiters (no `/.../`), just the body. Flags: `g` (global, all matches), `i` (case-insensitive), `m` (multiline, ^$ match line start/end), `s` (dotAll. matches newline too), `u` (unicode), `y` (sticky).
2. **Paste the test string** - Free text, multi-line OK. The tester uses the browser's native `RegExp` engine (V8 / SpiderMonkey / JavaScriptCore), so syntax and perf match standard JS.
3. **Read the results** - Output: original string with matches highlighted in yellow, statistics (total matches, capture groups), match list with index. For capture groups: each match also shows captured groups with their index.
4. **Pattern library** - 'Pattern library' button opens a categorized list of ready patterns: RFC-friendly email, HTTP/HTTPS URL, IPv4/IPv6, MAC address, Italian phone (landline and mobile), Italian fiscal code, VAT number, IBAN, ISO 8601 date, URL-safe slug. Click to load them in the pattern field.

## JavaScript regex: why a dedicated tester

**Differences across regex dialects.** JavaScript regexes are ECMAScript-compliant but differ from PCRE (PHP, Perl), POSIX (grep, sed).NET, Python `re`. Example: variable-length lookbehinds are JS-supported since 2018 (ECMAScript 2018), missing in old browsers. Atomic groups `(?>...)` missing in JS (present in PCRE/Perl). Recursive patterns missing in JS. For reliable regex testing on complex patterns, use a tester from the same family as the destination runtime.

**What this tool offers.** Tester with browser-native **RegExp**, so it represents exactly what happens in production JS code. Match highlight in yellow for visualization. Numbered and named capture groups (syntax `(?<name>...)`). Match index. Pattern library of 12+ common regexes (Italian-specific included) ready to use.

**Performance caveat.** The tool does not detect catastrophic backtracking. Patterns like `(a+)+` on long input freeze the browser tab. If the tester appears frozen: close the tab. For backtracking audit: [redos-checker](https://devina.io/redos-checker) or dedicated tools. In production, use [RE2](https://github.com/google/re2) (linear-time, no backtracking) for regexes on untrusted input.

## Glossary

- **Regex**: Regular expression: formal pattern for string matching. Invented in the 60s (Stephen Kleene). Modern implementations: PCRE (Perl Compatible), ECMAScript (JavaScript), POSIX (grep/sed), RE2 (Google, linear-time).
- **Capture group**: Sub-expression in parentheses `(...)` that captures a substring. Numbered from 1 (group 0 = full match). Named group: `(?<name>...)`. Non-capturing: `(?:...)`.
- **Lookahead / Lookbehind**: Position assertions. `(?=...)` positive lookahead (match followed by). `(?!...)` negative lookahead. `(?<=...)` lookbehind. Variable-length supported in JS since 2018.
- **Backtracking**: Regex engine behavior when a partial match fails: it goes back and tries alternatives. Patterns like `(a+)+` on input `aaaaab` have exponential complexity (catastrophic backtracking).
- **RE2**: Google's regex engine with linear-time complexity. No backtracking, no lookbehind, no backreferences. Safe for regexes on untrusted input (no DoS via catastrophic backtracking). Used by Google, Cloudflare, GitHub Code Search.
- **Sticky flag (y)**: JS flag that restarts matching from the exact `lastIndex` position (no advancing). Useful for tokenizing parsers. Different from `g` which can skip non-matchable characters.

## Frequently asked regex questions

### How do I use a regex with flags in production JS?

Two syntaxes: literal `/pattern/flags` or constructor `new RegExp('pattern', 'flags')`. Literal is compiled at parse-time (no runtime overhead). Constructor is useful for dynamic patterns (e.g. user input). Caveat: in the constructor, escape sequences must be doubled (`'\\d'` for `\d`).

### My regex is slow on long input: what to do?

Possible catastrophic backtracking. Typical offenders: `(a+)+`, `(.*)*`, alternations with common prefix. Three strategies: (1) refactor to a linear pattern (e.g. `a+` instead of `(a+)+`); (2) atomic groups or possessive quantifiers (NOT available in JS, only PCRE); (3) use RE2 server-side for untrusted input (Node.js: `re2` npm package; Go: `regexp` standard library).

### Differences between JS regex vs PHP/Python?

JS ECMAScript: no atomic groups, no recursive, no possessive quantifiers, no backreferences in lookbehind. PHP PCRE / Python re: all supported. Example: pattern `(?P<name>...)` Python does NOT work in JS (different Python syntax). JS uses `(?<name>...)` ES2018+.

### Does a 100% RFC-compliant email regex exist?

Technically: a [RFC 5322](https://www.rfc-editor.org/rfc/rfc5322) compliant regex is ~6,000 chars. In practice: nobody uses it. Pragmatic pattern (in library): `^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$` covers 99.99% of real cases. For rigorous validation: send confirmation email + click link.

### Italian CF pattern: what does it actually check?

Only **format** (16 alphanumeric chars in the expected pattern). NOT the CIN (control character). For full validation including CIN: use the [Italian ID validator](/tools/...) on this site (italian-only, no EN equivalent). A regex cannot compute the CIN modulo-26 sum.

### How to test regex with multiline input?

Flag `m` enables `^` and `$` to match at start/end of each line (not just full string). Without `m`, `^` matches only the very start of input. Example: `^abc` with `m` matches 'abc' at every line start.

### Can I use unicode property classes (\p{...})?

Yes, with the `u` (Unicode) flag enabled. Syntax: `\p{Letter}` matches any Unicode letter (including accents, Cyrillic, etc). `\P{Letter}` NON-letter. Supported in all modern browsers (2020+).

---
Tool by Maurizio Fonte, senior backend engineer. All tools: https://www.mauriziofonte.it/tools/
