RegexBuilder · regex answers
Regex word boundary explained
/\bcat\b/gi
\b is a zero-width position between a word character and a non-word character. It matches cat in 'the cat sat' but not inside 'concatenate', which is the usual reason a naive search-and-replace corrupts a file.
What each part does
| \b | a boundary — matches a position, consumes nothing |
| cat | the literal word |
| \b | the boundary on the other side |
Tested against real input
Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.
| Input | Result | Matched |
|---|---|---|
| the cat sat | matches | cat |
| CAT | matches | CAT |
| a cat. | matches | cat |
| concatenate | no match | — |
| category | no match | — |
| bobcat5 | no match | — |
The mistake to avoid
Using \b next to punctuation and expecting it to hold. In 'cat-like' there is a boundary after cat, so \bcat\b matches — because the hyphen is a non-word character.
Try it on your own text
Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.
Open RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID