Word Boundary (\b
) in JavaScript
The \b
is a special word boundary anchor in JavaScript regular expressions. It does not match any actual character but asserts whether a character on one side of the boundary is a word character (like letters, digits, or underscores) and the other side is not (such as spaces, punctuation, or the start or end of the string).
1. What is a Word Boundary?
A word boundary matches the position between a word character (\w
) and a non-word character (\W
). Word characters are usually:
- Letters (
a-z
,A-Z
) - Digits (
0-9
) - Underscores (
_
)
Non-word characters are everything else, such as spaces, punctuation, and the boundaries of the string.
The \b
anchor is useful when you want to match a pattern that appears as a whole word and not as part of another word.
2. Basic Example
The \b
is often used to ensure that the match occurs at the boundary of words. Here's a simple example:
In this example:
\bcat\b
ensures that "cat" is a whole word.- In
str1
, "cat" is a standalone word, so the match succeeds. - In
str2
, "cat" is part of "catapult", so it does not match as a whole word.
3. Word Boundary at the Start or End of a String
The \b
anchor can also match word boundaries at the beginning or end of a string.
Example: Match Word at the Beginning of a String
Here:
\bstart
matches "start" only when it is at the beginning of a word (not preceded by a non-word character).
Example: Match Word at the End of a String
In this case:
end\b
matches "end" only when it is at the end of a word (not followed by a non-word character).
4. Word Boundary in the Middle of a String
You can use \b
to match a word boundary anywhere in the middle of a string. Here's an example where we use it to match whole words in the middle of a sentence:
Example: Match Whole Words in a Sentence
In this case, \bapple\b
ensures that "apple" is treated as a standalone word and not as part of another word.
5. Practical Use Cases of \b
(Word Boundary)
a) Matching Full Words in a List
If you're validating input or searching for specific words, you can use \b
to ensure you only match whole words.
Here, it will match "hello" as a complete word, even if it appears multiple times in the string.
b) Preventing Partial Matches
When you're looking for a specific word within a string, you might not want partial matches (e.g., matching "cat" inside "catalog"). Using \b
ensures you match only the whole word:
c) Matching Digits as Separate Words
You can also use \b
to match standalone numbers:
6. Conclusion
\b
is the word boundary anchor in JavaScript regular expressions.- It matches the position between a word character (
\w
) and a non-word character (\W
), ensuring that the pattern matches whole words and not parts of words. - You can use
\b
to match words at the start, end, or middle of a string. - This is useful for tasks like validating complete words, preventing partial matches, and searching for specific word boundaries in a text.
By using \b
, you can make your regular expressions more precise and avoid false matches in longer strings.