# Word boundary
Another position check is a *word boundary* \b
. It doesn't match a character, but matches in situations when a wordly character follows a non-wordly or vice versa. A "non-wordly" may also be text start or end.
[cut]
For example, \bdog\b
matches a standalone dog
, not doggy
or catdog
:
```js
//+ run
showMatch("doggy catdog dog", /\bdog\b/) // "dog"
```
Here, dog
matches, because the previous char is a space (non-wordly), and the next position is text end.
Normally, \w{4}
matches 4 consequent word characters.
If the word is long enough, it may match multiple times:
```js
//+ run
showMatch("Boombaroom", /\w{4}/g) // 'Boom', 'baro'
```
Appending \b
causes \w{4}\b
to match only at word end:
```js
//+ run
showMatch("Because life is awesome", /\w{4}\b/g) // 'ause', 'life', 'some'
```
**The word boundary \b
like ^
and $
doesn't match a char. It only performs the check.**
Let's add the check from another side, \b\w{4}\b
:
```js
//+ run
showMatch("Because life is awesome", /\b\w{4}\b/g) // 'life'
```
Now there is only one result life
.
\b
at zero position:
\w{4}
, but fails to match finishing \b
.
\b
is right after Because
(position 9):
\w{4}
doesn't match, because the next character is a space.
\b
is right before life
at position 11.
\w{4}
matches and the position check \b
after it is positive. We've got the result.
/\b/
works only for words in latin alphabet,** because it is based on \w
as "wordly" chars. Sometimes that's acceptable, but limits the application range of the feature.
And, for completeness..
**There is also an inverse check \B
, meaning a position other than \b
.** It is extremely rarely used.