The word boundaryLa limite de mot
A space on either side of a word is the obvious way of delimiting it, but it
fails as soon as the word touches anything but a space: a word can be followed by a comma, preceded by an apostrophe,
or come at the start of a line. \b detects all of those boundaries at
once. It matches the position between a “word” character (\w, so an
unaccented letter, a digit or an underscore) and a character that is not one (a
space, punctuation, the edge of the text).
Like the anchors ^ and $, \b matches no character: it marks a position.
/\bcat\b/ finds the word “cat” on its own, not “catalogue” or “bobcat”.
The Cat eats. It is Small but Agile.Le Chat mange. Il est Petit mais Agile. L C I P A T C I S A The organisations UNO, UNESCO and NASA are important. The WTO and the FBI too. The word USA is well known. EU and USSR as well.Les organisations ONU, UNESCO et NASA sont importantes. L'OMC et le FBI aussi. Le mot USA est connu. UE et URSS aussi. ONU OMC FBI USA UNO WTO FBI USA The authors J.K. Rowling, C.S. Lewis and George Orwell are famous. You also find J. Austen, J-P. Sartre, E.B. White, T.S. Eliot and F.D.R. Roosevelt.Les auteurs J.K. Rowling, C.S. Lewis et George Orwell sont célèbres. On trouve aussi J. Austen, J-P. Sartre, E.B. White, T.S. Eliot et F.D.R. Roosevelt. J.K. C.S. E.B. T.S. F.D. J.K. C.S. E.B. T.S. F.D. Une espace de chaque côté d’un mot, c’est la façon évidente de le délimiter,
mais elle échoue dès que le mot touche autre chose qu’une espace : un mot peut être suivi d’une virgule, précédé
d’une apostrophe, ou placé en début de ligne. Le \b détecte toutes ces
limites d’un coup. Il correspond à la position entre un caractère « de mot »
(\w, donc lettre non accentuée, chiffre ou trait de soulignement) et un
caractère qui n’en est pas un (espace, ponctuation, bord du texte).
Comme les ancres ^ et $, \b ne correspond à aucun caractère : il marque
une position. /\bchat\b/ trouve le mot « chat » seul, pas « chatouiller » ni
« achat ».
The Cat eats. It is Small but Agile.Le Chat mange. Il est Petit mais Agile. L C I P A T C I S A The organisations UNO, UNESCO and NASA are important. The WTO and the FBI too. The word USA is well known. EU and USSR as well.Les organisations ONU, UNESCO et NASA sont importantes. L'OMC et le FBI aussi. Le mot USA est connu. UE et URSS aussi. ONU OMC FBI USA UNO WTO FBI USA The authors J.K. Rowling, C.S. Lewis and George Orwell are famous. You also find J. Austen, J-P. Sartre, E.B. White, T.S. Eliot and F.D.R. Roosevelt.Les auteurs J.K. Rowling, C.S. Lewis et George Orwell sont célèbres. On trouve aussi J. Austen, J-P. Sartre, E.B. White, T.S. Eliot et F.D.R. Roosevelt. J.K. C.S. E.B. T.S. F.D. J.K. C.S. E.B. T.S. F.D. Sources
- MDN Web Docs (n.d.). Regular expressions. MDN JavaScript Guide, Mozilla. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
Sources
- MDN Web Docs (n.d.). Regular expressions. MDN JavaScript Guide, Mozilla. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions