What `.`, `\d`, `\w`, `\s` and `[...]` match in Go, Python, JavaScript and PCRE2, which characters need escaping inside and outside a class, and why `\d` is ASCII-only everywhere except Python.
The problem
A character class looks like the simplest part of a regex, yet the same `\d` or `.` selects different text in different engines, and the rules for what must be escaped change once you are inside `[...]`. The bugs are quiet: a validator that accepts Arabic-Indic digits in Python, a `.` that stops at `\r` in JavaScript, a `[\w-.]` that compiles in Go and fails in Python.
How it works
- Shorthand classes: `\d` digits, `\w` word characters (`[A-Za-z0-9_]` in ASCII mode), `\s` whitespace, and the negations `\D \W \S`. Observed on Go 1.24, Node 22 and PCRE2 10.42: `\d+` on `x٣٤42` matches only `42`. Python 3.13 on a `str` pattern is Unicode-aware and matches `٣٤42`; `re.ASCII` or `[0-9]` restores the ASCII behavior.
- `\w` follows the same split. Go, JavaScript and PCRE2 match `[A-Za-z0-9_]` only (`\w+` on `café` gives `caf`), Python matches letters and digits of any script (`café`, and `中文abc` as one run). For Unicode letters use `\p{L}` (Go, PCRE2, JavaScript with `u`), not `\w`.
- `\s` differs in the corners. Go's `\s` is `[\t\n\f\r ]` and does not match a vertical tab or U+00A0. PCRE2 adds the vertical tab (`\x0B`) but not U+00A0. Python and JavaScript also match U+00A0 and spaces such as U+2003.
- `.` matches any character except a line terminator. Go, Python and PCRE2 (newline = LF) exclude only `\n`; JavaScript also excludes `\r`, U+2028 and U+2029 (`.+` on `ab\rcd` matches `ab`, elsewhere the whole string). The `s` flag (dotAll) lets `.` match everything; `[\s\S]` does the same in every engine.
- Inside `[...]`: `^` negates only as the first character, `-` is a range operator unless first or last, and `]` closes the class unless it comes first. `[]a]` matches `]` in Go, Python and PCRE2, but in JavaScript `[]` is an empty class that never matches. `[^]` is a compile error in Go, Python and PCRE2 and means "any character" in JavaScript.
- A shorthand cannot be a range endpoint: `[\w-.]` compiles in Go and in JavaScript without `u` (hyphen literal) but fails in Python (`bad character range \w-. at position 1`) and PCRE2 (`invalid range in character class`), and in JavaScript with `u` or `v` (`Invalid character class`). Write `[\w.-]`. `[\d.]` works everywhere. `[\b]` is backspace in Python, JavaScript and PCRE2 and a compile error in Go (`invalid escape sequence: `\b``).
- POSIX classes such as `[[:alpha:]]` work in Go and PCRE2; Python and JavaScript do not parse them as POSIX (`[[:alpha:]]+` on `ab1` finds nothing). Class subtraction like `[a-z&&[^b]]` is not supported by any of the four (only JavaScript's `v` flag has set operations, with `--` and `&&`).
- Escaping outside a class: escape `\ . * + ? ( ) [ { | ^ $`. A bare `* + ? ( )` or `[` is a compile error in all four engines, while a bare `.`, `|`, `^` or `$` silently keeps its operator meaning. A bare `]` or `}` is literal, except in JavaScript with `u` or `v`, where both are errors (`Lone quantifier brackets`).
- Unknown escapes: Go, Python and PCRE2 reject escaped ASCII letters they do not define (`\y` gives `invalid escape sequence: `\y``, `bad escape \y at position 0`, `unrecognized character follows \`); JavaScript without `u` quietly reads `\y` as `y` and `\e` as `e`. With `u`, `\-`, `\#`, `\ ` and `\&` outside a class are errors (`Invalid escape`).
- Spelling a code point: `\x41` works in all four; `\x{41}` in Go and PCRE2 only; `\u{41}` in JavaScript with `u` only; the four-digit `\u` form (backslash, `u`, four hex digits) in Python and JavaScript only (Go and PCRE2 reject it). `\Q...\E` literal spans work in Go and PCRE2; PCRE2 also adds `\h`, `\R`, `\K`, `\e` (Go, Python: error; JavaScript: matches the plain letter).
Examples
import (
"fmt"
"regexp"
)
func demo() {
const in = "x٣٤42 café"
fmt.Printf("%q\n", regexp.MustCompile(`\d+`).FindString(in))
fmt.Printf("%q\n", regexp.MustCompile(`\p{Nd}+`).FindString(in))
fmt.Printf("%q\n", regexp.MustCompile(`\w+`).FindAllString(in, -1))
fmt.Printf("%q\n", regexp.MustCompile(`[\p{L}\p{Nd}]+`).FindAllString(in, -1))
fmt.Printf("%q\n", regexp.MustCompile(`[[:alpha:]]+`).FindString("ab1"))
_, err := regexp.Compile(`[\b]`)
fmt.Println(err)
}
// Output on Go 1.24:
// "42"
// "٣٤42"
// ["x" "42" "caf"]
// ["x٣٤42" "café"]
// "ab"
// error parsing regexp: invalid escape sequence: `\b`Python: `\d` and `\w` are Unicode-aware by default; `re.ASCII` or an explicit range restores ASCII.
import re
s = "x٣٤42"
print(re.findall(r"\d+", s))
print(re.findall(r"\d+", s, re.ASCII))
print(re.findall(r"[0-9]+", s))
print(re.findall(r"\w+", "café 中文"))
print(re.findall(r"\w+", "café 中文", re.ASCII))
# Output on Python 3.13:
# ['٣٤42']
# ['42']
# ['42']
# ['café', '中文']
# ['caf']JavaScript: `\d` and `\w` are ASCII-only, `\s` includes U+00A0, and Unicode needs `\p{...}` with the `u` flag.
const s = "x٣٤42";
console.log(s.match(/\d+/g));
console.log(s.match(/\p{Nd}+/gu));
console.log("café 中文".match(/\w+/g));
console.log("café 中文".match(/[\p{L}\p{N}]+/gu));
console.log(/\s/.test("a\xa0b"), /\s/.test("a\xa0b".replace("\xa0", "")));
// Output on Node 22:
// [ '42' ]
// [ '٣٤42' ]
// [ 'caf' ]
// [ 'café', '中文' ]
// true falseIn practice
- Go regexp — RE2 syntax: `\d \w \s \b` are ASCII-only by design, Unicode needs `\p{...}`, `[[:alpha:]]` works, and `\Z`, `\h`, `\R` do not compile. Observed on Go 1.24.
- Python re — `str` patterns use Unicode semantics for `\d \w \s \b` unless `re.ASCII` is set (observed on 3.13: `\d+` matches `٣٤`). `bytes` patterns are ASCII-only (observed: `rb'\w+'` on the UTF-8 bytes of `café` matches `caf`).
- V8 Irregexp — ECMAScript rules: `.` stops at `\r`, U+2028 and U+2029, `\s` includes U+00A0, and without `u` unknown identity escapes are allowed (`\y` matches `y`). Observed on Node 22.22.
- GNU grep -P — Uses PCRE2 10.42 here. In a UTF-8 locale `printf '\xd9\xa3\n' | grep -cP '\d'` prints `0`, while `\p{Nd}` prints `1`: `\d` stays ASCII-only, so the property escape is the Unicode spelling.
- ripgrep — Rust regex semantics, a fifth engine this sheet does not tabulate: `\d` is Unicode-aware by default (`echo 'x٣٤' | rg -o '\d+'` prints `٣٤`) and `(?-u)\d+` restores ASCII. Observed on rg 14.1.0.
Trade-offs
Pros
- Shorthand classes are short and, for ASCII input, behave the same in every engine.
- Explicit ranges such as `[0-9]` and `[A-Za-z]` mean the same thing everywhere, including Python.
- The set of characters that need escaping is the same twelve in all four engines, so one `QuoteMeta`-style function is portable.
Cons
- `\d \w \s \b` are not portable for non-ASCII text: Python is Unicode-aware, the other three are not.
- Leniency hides typos: a bare `{` or `]` compiles outside JavaScript's `u` mode, and JavaScript without `u` accepts `\y`.
- Class-internal corner cases (`[]a]`, `[^]`, `[\w-.]`, POSIX classes) differ, so a pattern that compiles in one engine may fail in another.