Regex flags across languages
Use when writing, porting or debugging a regular expression and its flags/modifiers across languages and tools - JavaScript (g i m s u v y d), PCRE/PHP (i m s x u A D U J), Python re (re.I, re.M, re.S, re.X, re.A), .NET (RegexOptions), POSIX grep/sed. Also use for Laravel `regex:` validation rules and for catastrophic backtracking risks.
Licence MIT
Source delaparra-original
Download zipRegex flags across languages
The same regex can behave differently depending on the engine and its flags. Identify the target engine first, then write the pattern and flags for that engine, and test it with matching and non-matching examples.
Workflow
- Name the engine: JavaScript, PHP (PCRE2), Python
re, .NET, Java, or POSIX (grep,sed). Say which one you are writing for. - Write test cases first: at least 3 strings that must match and 3 that must not (including empty string and edge cases).
- Write the pattern and flags, anchored where possible (
^…$or\A…\z). - Test in the target language (a one-line script or unit test), not only in an online tester set to another flavour.
- Check for backtracking risk: nested quantifiers like
(a+)+,(.*)*or(\w+\s?)*$can take exponential time on long input (ReDoS). Prefer specific character classes and possessive/atomic groups where supported.
Flag reference
| Meaning | JavaScript | PHP / PCRE | Python | .NET | POSIX grep |
|---|---|---|---|---|---|
| Case-insensitive | i | i | re.I | IgnoreCase | grep -i |
^/$ match at line breaks | m | m | re.M | Multiline | default per line |
. matches newline (dotall / single-line) | s | s | re.S | Singleline | n/a |
| Ignore whitespace, allow comments (extended / verbose) | n/a | x | re.X | IgnorePatternWhitespace | n/a |
| Unicode-aware | u (or v for Unicode sets) | u | default for str | default | locale |
| ASCII-only classes | n/a | n/a | re.A | ECMAScript | n/a |
| All matches (global) | g | use preg_match_all | use re.findall / finditer | Matches() | default |
| Sticky (match at lastIndex only) | y | \G | n/a | \G | n/a |
| Match indices | d (hasIndices) | PREG_OFFSET_CAPTURE | m.span() | Match.Index | grep -b |
| Anchored at start | n/a | A | re.match | \G | n/a |
$ only at very end | n/a | D (dollar-end-only) | use \Z | use \z | n/a |
| Ungreedy by default | n/a | U (swaps * and *?) | n/a | n/a | n/a |
| Duplicate named groups | n/a | J | n/a | allowed | n/a |
| Explicit capture only | n/a | n (PCRE2 10.30+, check version) | n/a | ExplicitCapture | n/a |
| Right-to-left | n/a | n/a | n/a | RightToLeft | n/a |
Notes: PHP's old e modifier (eval) is removed; use preg_replace_callback. Perl's o (compile once) and l (locale) modifiers rarely matter in modern code.
Laravel validation
'postcode' => ['required', 'regex:/^[1-9][0-9]{3}\s?[A-Z]{2}$/i'],
Use array syntax when the pattern contains |, otherwise Laravel splits the rule string. Include delimiters and flags inside the string.
Rules
- Don't use regex to parse HTML, JSON or full email/URL syntax; use a parser or the framework's validator (
email,url). - Always return the test cases along with the final pattern.
Files
LICENSESKILL.mdSOURCE.md
Related terms
- /A (Anchored)
- /a (ASCII-only)
- /D (Dollar End Only)
- /d (hasIndices)
- /e (Eval (deprecated))
- /g (Global)
- /I (Case-Insensitive (POSIX / grep))
- /i (Case-Insensitive)
- /J (Duplicate Named Groups)
- /L (Locale-dependent)
- /m (Multiline)
- /n (Explicit Capture)
- /o (Once (interpolate once))
- /r (Right-to-Left)
- /s (DotAll (single line))
- /U (Ungreedy)
- /u (Unicode)
- /v (Unicode Sets)
- /x (Extended / Verbose)
- /y (Sticky)
Installing an agent skill in Claude Code, Codex, Cursor and Copilot