Regex Cheat Sheet

Step 1 / 3 33%

Choose a regex flavour

Start with the engine you use. The reference will hide syntax that does not apply to that choice.

Use this interactive reference to compare common regular-expression syntax before you paste it into code. Pick JavaScript, PCRE2/PHP, Python re, .NET, or an all-engine view; then choose a topic and search the displayed entries. The reference marks syntax that is specific to one family, because a token that compiles in one engine can be rejected or mean something different in another. Searching and copying happen in your browser, and this page does not run patterns against your text.

How to use the regex cheat sheet

  1. 1

    Choose an engine

    Start with JavaScript, PCRE2/PHP, Python re, .NET, or compare all of them. The focused view omits entries that do not apply to the selected engine.

  2. 2

    Choose a topic

    Open anchors, character classes, quantifiers, groups, lookarounds, common pattern shapes, or flags and options.

  3. 3

    Search and copy locally

    Search tokens and descriptions directly in the page, then select a token to copy it. The search text is not sent to the server or added to the URL.

  4. 4

    Verify in the real runtime

    Use the matching engine, flags and API in your project to compile and test the final expression. This reference explains syntax; it does not validate production data.

Compatibility matters

Regular expressions are not one universal language. The quick reference separates the syntax that looks similar but does not travel cleanly between engines.

Feature JavaScript RegExp PCRE2 / PHP Python re .NET
Absolute start No \A \A \A \A
Absolute end No \z \z \Z \z
Unicode property \p{L} Use u or v mode Supported Not in built-in re Supported
Named capture (?<name>...) (?<name>...) or (?P<name>...) (?P<name>...) (?<name>...)
Named backreference \k<name> \k<name> or (?P=name) (?P=name) \k<name>
Global matching g flag plus a matching API Repeated-match API such as preg_match_all Repeated-match API such as finditer Repeated-match API such as Matches

\d and \w deserve special care. In JavaScript, \d remains the ASCII digits 0-9, and \w is essentially ASCII word characters even when you add u. Other engines can apply Unicode-aware rules depending on their mode and configuration. If Unicode behaviour matters, write and test the exact property or class you need.

Anchors and boundaries

Token What it expresses Portability note
^ Start of input, or a line in multiline mode Available in all four families.
$ End of input, or a line in multiline mode Available in all four families; final-newline details differ.
\b Word boundary What counts as a word character depends on the engine and mode.
\A Absolute subject start PCRE2, Python and .NET; not JavaScript.
\z Absolute subject end PCRE2 and .NET; Python uses \Z.
\Z Python subject end PCRE2 and .NET can also match before a final line break; use \z there for absolute end.

Groups and lookarounds

Token Meaning Important limit
(abc) Capturing group Available in all four families.
(?:abc) Non-capturing group Available in all four families.
(?<name>abc) Named group JavaScript, PCRE2 and .NET syntax.
(?P<name>abc) Named group Python syntax; PCRE2 also accepts it.
(?=abc) / (?!abc) Positive / negative lookahead Available in all four families.
(?<=abc) / (?<!abc) Positive / negative lookbehind All four support it, but permitted lookbehind lengths differ.

Lookbehind is a common portability trap. Python re needs a fixed-width lookbehind. JavaScript support depends on the runtime you deploy. Test the exact pattern in the oldest runtime you support rather than relying on a cheat-sheet row alone.

Pattern shapes, not full validators

The common-pattern entries intentionally describe shapes. For example, ^\d{4}-\d{2}-\d{2}$ accepts 2026-99-99, and a simple email-shaped pattern cannot implement the full email-address standard. Parse dates, URLs and structured identifiers with the right library after an initial format check.

Shape Example pattern What it does not prove
Signed decimal ^-?\d+(?:\.\d+)?$ Locale decimal marks, grouping, numeric range or precision.
Email-shaped text ^[\w.+-]+@[\w-]+\.[\w.-]+$ A deliverable or standards-complete email address.
HTTP(S) URL-shaped text ^https?://\S+$ A safe, parseable or reachable URL.
ISO-date shape ^\d{4}-\d{2}-\d{2}$ A real calendar date.
ASCII slug characters ^[A-Za-z0-9_-]+$ Your application's casing, separator or reserved-word rules.

Flags and options

Do not copy a flag without checking the host API. JavaScript writes /pattern/g, while Python passes re.I or re.X, .NET can use RegexOptions, and PCRE2/PHP commonly uses delimiters and trailing modifiers. g is a JavaScript global-match flag, not a PCRE2 modifier; repeated PCRE2 matching is usually controlled by the PHP API. Extended mode (x) is available in PCRE2, Python and .NET, but not as a JavaScript flag.

Frequently Asked Questions

No. It is a syntax reference with browser-local search and copy controls. Use a regex tester and the runtime used by your application to compile patterns and test representative inputs.

Some familiar-looking tokens are not portable. For example, Python built-in re uses (?P<name>…) for named groups, JavaScript uses (?<name>…), and JavaScript does not support the absolute anchors \A or \z.

No. In JavaScript, \d is ASCII digits and \w is essentially ASCII word characters even with the u flag. Other engines have different Unicode modes and options, so test the behaviour needed by your data.

Treat them as quick shape checks only. They do not prove that an email address is deliverable, a URL is safe or reachable, or a date exists on the calendar. Use a purpose-built parser or validator for production rules.

Related Tools