Word Boundary (Whole Word Match) in JS
Match the whole word `cat` without matching it inside other words like `concatenate` or `category`.
Try it in the JS tester →Pattern
regexJS
\bcat\b (flags: gi)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("\\bcat\\b", "gi");
const input = "The cat sat on the mat.";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
\b is a zero-width assertion at a word boundary — the position between a word character (\w) and a non-word character (or string edge). Wrapping `cat` in two \b anchors forces the match to start AND end at word boundaries, so `cat` matches but `concat` and `cats` do not.
Examples
Input
The cat sat on the mat.Matches
cat
Input
I will concatenate the strings.No match
—Input
CAT, Cat, cat — all match.Matches
CATCatcat