Word Boundary (Whole Word Match)
Match the whole word `cat` without matching it inside other words like `concatenate` or `category`.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
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).
Python (re) code
import re
pattern = re.compile(r"\bcat\b", re.IGNORECASE)
input_text = "The cat sat on the mat."
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)\bcat\b`)
input := `The cat sat on the mat.`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
\bcat\b (flags: gi)Raw source: \bcat\b
How it works
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
Common use cases
- •Find-and-replace targeting whole words only
- •Search-engine-style keyword highlighting
- •Linting for forbidden whole-word identifiers
- •Tokenizer fallback in NLP pipelines
Related patterns
Duplicate Word
Text ProcessingDetects consecutive duplicate words using a backreference.
Sentence Boundary
Text ProcessingMatches sentence boundaries (punctuation followed by whitespace and a capital letter).
Hashtag
Text ProcessingMatch hashtags (# followed by word characters) in social media posts, including accented Latin characters.
HCL / Terraform Variable Reference
Text ProcessingMatch Terraform / HCL references like `var.name`, `local.foo`, `module.x.output`, or `data.aws_ami.ubuntu.id`.