\b and accented letters\b et les lettres accentuées
The accents page showed how
you can overcome the limitations of \w by using \p{L} instead, which does
include accented letters. \b has no such way out: it stays defined by \w
whether or not the u flag is on, and there is no Unicode property to put in
its place.
The consequence is concrete, and easier to see than to explain.
Figure 1figure 1 shows one sentence twice, with the word boundaries
a reader sees above and the ones \b sees below. Since “é” and the other accented letters are not word
characters for \b, three things happen:
- An accented letter at the start or end of a word removes the boundary on
that side, whether it touches a space (“thé”, “à”) or punctuation (the
apostrophe in “l’été”, the comma after it): between two characters that are
not word characters,
\bsees no boundary. - An accented letter inside a word puts a boundary on each side of it, where there is none (“très”, “hôtel”, the “t” of “été”).
- Words without accents (“un”, “chaud”) are delimited correctly.
A search for a whole word shows it most directly. Between two \b, a word
without accents is found as intended, but a word ending in “é” is not, and its
letters are found at the start of a longer word instead (the examples are
French words):
| Expression | Text | Match |
|---|---|---|
/\bthym\b/ | “du thym,” | “thym” |
/\bthé\b/ | “un thé,” | none: no boundary between “é” and the comma |
/\bthé\b/ | “la théorie” | “thé”, at the start of “théorie”: there is a boundary between “é” and “o” |
/\bcafé\b/Test text (French): Un café, deux cafés et la cafétéria.
Which matches will be found?
/\bcafé\b/Texte de test : Un café, deux cafés et la cafétéria.
Quelles correspondances seront trouvées ?
Show the solutionVoir la solution
\b, "é" is not a word character. After "café", the comma is not one either: no boundary, so the word on its own is not found. In "cafés" and "cafétéria", the letter after "é" is a word character: there is a boundary in the middle of the word, and the expression finds "café" there.\b, « é » n'est pas un caractère de mot. Après « café », la virgule n'en est pas un non plus : pas de limite, donc le mot seul n'est pas trouvé. Dans « cafés » et « cafétéria », la lettre qui suit « é » est un caractère de mot : il y a une limite au milieu du mot, et la regex y trouve « café ».\b and \p{L}, then compare the matches with what you expected: which ones are missing, and why? The test text stays French, because the lesson is what \b does to accented letters.\b et \p{L}, puis comparez les correspondances à ce que vous attendiez : lesquels manquent, et pourquoi ?Il a été très fier. Son thé est là. Elle dit que non. Le mot 'car' aussi.Il a été très fier. Son thé est là. Elle dit que non. Le mot 'car' aussi. trè Son est dit que non mot car trè Son est dit que non mot car La page sur les accents a
montré comment contourner les limites de \w en utilisant \p{L}, qui inclut
les lettres accentuées. Le \b, lui, n’a pas d’échappatoire : il reste défini
par \w, que le drapeau u soit actif ou non, et il n’existe aucune propriété
Unicode pour le remplacer.
La conséquence est concrète, et elle se voit mieux qu’elle ne s’explique.
La Figure 1figure 1 montre deux fois la même phrase, avec les limites de
mot que voit un lecteur en haut et celles que voit \b en bas. Comme « é » et les autres lettres accentuées ne sont
pas des caractères de mot pour \b, trois choses se produisent :
- Une lettre accentuée au début ou à la fin d’un mot efface la limite de ce
côté, qu’elle touche une espace (« thé », « à ») ou une ponctuation
(l’apostrophe de « l’été », la virgule qui le suit) : entre deux caractères
qui ne sont pas des caractères de mot,
\bne voit pas de limite. - Une lettre accentuée au milieu d’un mot place une limite de chaque côté, là où il n’y en a pas (« très », « hôtel », le « t » d’« été »).
- Les mots sans accent (« un », « chaud ») sont délimités correctement.
La recherche d’un mot entier le montre le plus directement. Entre deux \b,
un mot sans accent est trouvé comme prévu, mais un mot qui finit par « é » ne
l’est pas, et ses lettres sont trouvées au début d’un mot plus long à la
place :
| Expression | Texte | Correspondance |
|---|---|---|
/\bthym\b/ | « du thym, » | « thym » |
/\bthé\b/ | « un thé, » | aucune : pas de limite entre « é » et la virgule |
/\bthé\b/ | « la théorie » | « thé », au début de « théorie » : il y a une limite entre « é » et « o » |
/\bcafé\b/Test text (French): Un café, deux cafés et la cafétéria.
Which matches will be found?
/\bcafé\b/Texte de test : Un café, deux cafés et la cafétéria.
Quelles correspondances seront trouvées ?
Show the solutionVoir la solution
\b, "é" is not a word character. After "café", the comma is not one either: no boundary, so the word on its own is not found. In "cafés" and "cafétéria", the letter after "é" is a word character: there is a boundary in the middle of the word, and the expression finds "café" there.\b, « é » n'est pas un caractère de mot. Après « café », la virgule n'en est pas un non plus : pas de limite, donc le mot seul n'est pas trouvé. Dans « cafés » et « cafétéria », la lettre qui suit « é » est un caractère de mot : il y a une limite au milieu du mot, et la regex y trouve « café ».\b and \p{L}, then compare the matches with what you expected: which ones are missing, and why? The test text stays French, because the lesson is what \b does to accented letters.\b et \p{L}, puis comparez les correspondances à ce que vous attendiez : lesquels manquent, et pourquoi ?Il a été très fier. Son thé est là. Elle dit que non. Le mot 'car' aussi.Il a été très fier. Son thé est là. Elle dit que non. Le mot 'car' aussi. trè Son est dit que non mot car trè Son est dit que non mot car Sources
- Jurafsky, D., & Martin, J. H. (2026). Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition with Language Models. Online draft, 3rd edition. https://web.stanford.edu/~jurafsky/slp3/
- MDN Web Docs (n.d.). Regular expressions. MDN JavaScript Guide, Mozilla. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
Sources
- Jurafsky, D., & Martin, J. H. (2026). Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition with Language Models. Online draft, 3rd edition. https://web.stanford.edu/~jurafsky/slp3/
- MDN Web Docs (n.d.). Regular expressions. MDN JavaScript Guide, Mozilla. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions