Level 3: Advanced · Lesson 28 of 30
Why Regex Differs Between Languages
Every language ships its own regex engine with its own features and syntax. Learn the differences that most often break a pattern when you move it between JavaScript, Python, Go and others.
Regex looks like one language, but it is really a family of dialects. JavaScript, Python, PHP, Java, C#, Go, Rust and Ruby each ship their own engine, written at different times with different goals. The core is shared: literals, classes, \d, quantifiers, groups and alternation work almost everywhere. The differences appear in the advanced features, and those are exactly the features that make a pattern break when you copy it.
The biggest split is between backtracking engines and linear-time engines. JavaScript, Python, PHP, Java, C# and Ruby backtrack, which allows lookarounds and backreferences but exposes them to catastrophic backtracking. Go and Rust guarantee linear-time matching instead. As a result, Go and Rust have no lookahead, no lookbehind and no backreferences. A JavaScript pattern like (?<=\$)\d+ must become \$(\d+) with a capture group before it will even compile in Go.
Lookbehind itself varies. JavaScript (since ES2018) and C# allow variable-length lookbehind. Python requires fixed width, PHP requires each alternative to be fixed, Java requires a bounded length, and Ruby's support is limited.
Named groups use different spellings. JavaScript, C#, Ruby and PHP accept (?<name>...). Python's re accepts only (?P<name>...) and writes named backreferences as (?P=name). Java accepts (?<name>...) but only letters and digits in the name. Go and Rust long used (?P<name>...) and accept (?<name>...) in newer versions.
Flags are another trap. JavaScript has no inline flags, so (?i) at the start of a pattern is a syntax error; you pass i as a separate flag instead. Python, PHP, Java, C#, Go, Rust and Ruby all accept inline flags. The extended x flag, which allows whitespace and comments, is missing in JavaScript and Go. The sticky y flag exists only in JavaScript.
Character meanings shift too. Python's re has no \p{L}, while JavaScript needs the u flag for it. In JavaScript, Go, Java, PHP and Ruby, \d and \w default to ASCII, while Python, C# and Rust make them Unicode-aware. In Ruby, ^ and $ always match at line boundaries, so validation should use \A and \z. Possessive quantifiers and atomic groups exist in PHP, Java and Ruby, partly in Python 3.11+ and C#, and not at all in JavaScript, Go or Rust.
The table on this page summarises support for each feature. When you need a pattern to run in several languages, stick to the shared core, prefer capture groups over lookarounds, and use RegexLab's converter to check what changes for each target.
Feature comparison
| Feature | JavaScript | Python | PHP | Java | Go | C# | Rust | Ruby |
|---|---|---|---|---|---|---|---|---|
Lookahead (?= ) (?! ) | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | ✗ | ✓ |
Lookbehind (?<= ) (?<! ) | ✓ (ES2018) | fixed width only | each alternative fixed | bounded | ✗ | ✓ variable | ✗ | limited |
Backreference \1 | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | ✗ | ✓ |
Named group (?<name> ) | (?<n>) | (?P<n>) only | both | (?<n>) letters/digits only | (?P<n>), (?<n>) 1.22+ | (?<n>) | (?P<n>), (?<n>) 1.9+ | (?<n>) |
Named backreference \k<name> | \k<n> | (?P=n) | \k<n>, (?P=n) | \k<n> | — | \k<n> | — | \k<n> |
Possessive / atomic a++ (?>a) | ✗ | 3.11+ | ✓ | ✓ | ✗ | atomic (?>) only | ✗ | ✓ |
Unicode property \p{L} \p{L} | needs u | ✗ (re) | needs u | ✓ | ✓ | ✓ | ✓ | ✓ |
Inline flags (?i) | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Extended x flag x | ✗ | ✓ | ✓ | ✓ | ✗ | ✓ | ✓ | ✓ |
Sticky y flag y | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ |
\d and \w are ASCII-only by default in JavaScript, Go and Java, but Unicode-aware by default in Python, .NET and Rust.
Live tester
2 matches (JavaScript engine)
Mail ali@example.com and sara@example.com today.
Examples
Numbers
/\d+/Order 42 contains 7 items.
Matches: “42”, “7”
In JavaScript `\d` is ASCII only; in Python 3 string patterns it also matches other Unicode digits.
Text
/\p{L}+/uHello Ωmega world
Matches: “Hello”, “Ωmega”, “world”
Works in JavaScript with `u`, but Python's `re` has no `\p{L}` and needs a different approach.
Codes
/(?<code>[A-Z]{3})-\d+/Batches ABC-1 and XYZ-22 shipped.
Matches: “ABC-1”, “XYZ-22”
Python's `re` would need this named group written as `(?P<code>...)`.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Without using `\p{L}` (so it also works in Python `re`), match whole words made only of ASCII letters.
alpha beta2 gamma
Task 2: Without lookbehind (so it also works in Go), match dollar amounts including the `$` sign.
Pay $40 now, ref 99
Task 3: JavaScript has no inline `(?i)`. Match `error` in any letter case by using a flag instead.
ERROR error Error warn