Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Getting Started

Regex Flavors and Testing Tools

Regular expressions come in flavors. Compare JavaScript, PCRE, Python, Java and POSIX syntax, see what each supports, and learn how to test patterns safely with small tools and checklists.

"Regex" is not one single language. Every engine implements its own flavor: a dialect that agrees on the basics (\d, +, [a-z], groups) but differs on advanced features, escapes and defaults. A pattern that works in JavaScript may need a small change in Python or fail in a database. Knowing the flavors saves hours of confusion.

This course uses the JavaScript flavor for runnable examples, because it runs everywhere (browsers, Node.js, Deno, Bun). Where other flavors differ in important ways, we point it out with read-only code.

The main flavors

Common flavors

JavaScript ECMAScript RegExp
PCRE PHP, grep -P, nginx
Python re Close to PCRE
Java / .NET Rich and powerful

Same core, different extras

1 / 2

Here is a rough feature map. Treat it as a guide, not a contract: versions change.

Feature JavaScript PCRE / PHP Python re Java RE2 / Rust
Lookahead (?=...) yes yes yes yes no
Lookbehind (?<=...) yes (variable length) fixed length only fixed length only bounded length no
Named groups (?<n>...) (?<n>...) (?P<n>...) (?<n>...) yes
Backreferences \1 yes yes yes yes no
Possessive quantifiers a++ no yes yes (3.11+) yes no
Atomic groups (?>...) no yes yes (3.11+) yes no
Recursion (?R) no yes no no no
Unicode \p{L} with u / v flag yes no (needs regex package) yes yes
Inline flags (?i) no (only (?i:...) in new engines) yes yes yes yes

A feature that is missing in a flavor can often be rebuilt. For example, JavaScript has no possessive quantifiers, but a lookahead with a backreference acts like an atomic group. You will see that trick in a later lesson.

Spot the difference

The same task, written for two flavors. Only the JavaScript version runs here.

Node.js Named group in JavaScript

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Python
# Python spells the same group with a P
import re
m = re.search(r"(?P<year>\d{4})-(?P<month>\d{2})", "2026-11-12")
print(m.group("year"))   # 2026

Another everyday difference is flag syntax. JavaScript puts flags after the closing slash; PCRE, Python and Java also allow them inline.

TEXT
JavaScript:   /hello/i
PCRE/Python:  (?i)hello
Java:         Pattern.compile("hello", Pattern.CASE_INSENSITIVE)

Escaping in strings versus literals

Languages differ in how patterns are written, not only what they mean. JavaScript has regex literals. Python, Java and C# store patterns in strings, so backslashes need care.

TEXT
JavaScript literal:    /\d+\.\d+/
Python raw string:     r"\d+\.\d+"
Java string:           "\\d+\\.\\d+"

Python's raw string r"..." is the idiomatic way to avoid doubling backslashes. In Java you must double them; text blocks do not help with that.

Testing patterns safely

Good habits make regexes much less painful.

  1. Start with samples. Write down strings that must match and strings that must not. Include edge cases: empty string, extra whitespace, uppercase, very long input.
  2. Test the whole set at once, not only one sample.
  3. Grow the pattern in small steps. Add one piece at a time and re-run.
  4. Anchor when validating. A pattern without ^ and $ matches any substring, which is rarely what validation needs.
  5. Check how it fails. Make sure unmatched input returns false, not an exception.
Node.js A tiny regex test harness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The first pattern accepts 555-12345 because it finds 555-1234 inside it. Anchors fix that. A small harness like this one is the cheapest regex debugger there is.

Online testers and visualizers

Many websites let you paste a pattern and text and see matches highlighted, with explanations and a step-by-step debugger. They are handy for exploring, but remember:

  • Select the same flavor as your real language. A pattern that works in a PCRE tester may fail in JavaScript.
  • Do not paste secrets or private data into an online tool.
  • Copy the final pattern into automated tests; do not rely on a browser tab.

Command-line tools work too. grep -E uses POSIX extended syntax, grep -P uses PCRE, and Node.js gives you the JavaScript flavor in a one-liner.

Node.js Which engine am I using?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Feature detection like this is useful when you write libraries that must run on older engines.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a regex flavor?

    Each engine implements its own set of features and escapes. The common core is similar, but advanced features differ.
  2. 2. Which engine family guarantees linear-time matching by leaving out backreferences and lookaround?

    RE2-style engines avoid backtracking. That makes them safe against catastrophic patterns, at the cost of fewer features.
  3. 3. Why should a validation pattern use ^ and $?

    Without anchors, a pattern matches wherever it fits inside the text. A valid fragment inside invalid input would pass.
  4. 4. How do you write a named group year in Python's re module?

    Python uses the (?P<name>...) form. Newer JavaScript, Java and PCRE use (?<name>...).

Summary

  • Engines implement different flavors: JavaScript, PCRE, Python, Java, .NET, POSIX, RE2.
  • The core (classes, quantifiers, groups, anchors) is shared; lookbehind, named groups, possessive quantifiers and recursion differ.
  • Choose a tester that matches your target flavor, and test with both positive and negative samples.
  • Anchor validation patterns and keep a small harness of examples.

Next: Reading and Writing Your First Patterns