# Python `\b` is Unicode-aware, JavaScript's is ASCII-only
<!--
ref: @protocol/python-js-word-boundary-trap
type: protocol
tags: [python, javascript, typescript, regex, porting, unicode, traps]
triggers: ["porting a regex from python to javascript", "regex matches in one language and not the other", "word boundary behaves differently", "accented text matches when it should not"]
trigger_prompt: When translating a pattern between Python and JavaScript, or when a ported filter admits or drops a row the original did not.
version: 3
updated_at: 2026-10-24T21:09:20.132Z
-->
Porting a regex between the two languages looks like copying a string. One character does not survive it.
**Python:** `ī`, `é`, `ß` are word characters, so `Rīga` is a single word and `\bga\b` does not match inside it.
**JavaScript:** `\b` is defined over `[A-Za-z0-9_]` only — even with the `u` flag. `ī` is a non-word
character, so `Rīga` reads as `R|ī|ga`, a boundary appears out of nowhere, and `\bga\b` matches.
Measured, not imagined: a filter tested locations against a list of two-letter US state
codes, `\b(ca|ny|...|ga|...)\b`. Against 44,593 real records the ported pattern differed on exactly
one — **"Rīga, Latvia" entered the US corpus as Georgia.** Latvia was not on the excluded-countries
list, so nothing downstream would have caught it.
**One row is not the point.** Every pattern that is a list of short words or codes gets a new place to
match inside any word containing an accent, and the corpus is the open web, so accents are ordinary. A
diff of one row is the visible edge of a class of silent misclassification.
## Translate the boundary instead of trusting it
```ts
const WORD = "[\\p{L}\\p{N}_]";
const BOUNDARY = `(?:(?<!${WORD})(?=${WORD})|(?<=${WORD})(?!${WORD}))`;
const NON_BOUNDARY = `(?:(?<=${WORD})(?=${WORD})|(?<!${WORD})(?!${WORD}))`;
```
Rewrite `\b` → `BOUNDARY` and `\B` → `NON_BOUNDARY`, then compile with the `u` flag, which also brings
case-insensitive matching in line with Python's folding. **A `\b` inside a character class is a
backspace in both languages** — leave that one alone, which means the rewrite has to track whether it
is inside `[...]` rather than doing a blind string replace.
Two more that bite in the same pass:
- **`\p{L}` is only an escape under the `u` flag.** Without it, the pattern matches the literal text
`p{L}` and the regex quietly stops matching anything. Watch for this when a pattern is COMPOSED from
the `.source` of other patterns — the sources carry the property escapes, the composed flags do not.
- **`re.I` and `/i` fold differently** on a handful of characters (the Kelvin sign, dotted/dotless i);
`u` closes most of that gap.
## How to know, rather than hope
Run both implementations over the whole real corpus and diff the classification per record, not a
sample. The difference here was 1 in 44,593: a sample of a thousand would have agreed perfectly and
proved nothing. If the original is about to be deleted, generate a fixture from it first — its output
on real inputs — so the port stays pinned to what actually ran.
---
*Related: `@protocol/protocol-api-write-traps` · `@protocol/shell-and-runtime-traps` · `@protocol/vue-facing-decorator-traps` · `@protocol/postgres-timestamp-self-match` · `@skills/mcp-builder`*
---
*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*
*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*