By the end, you can use JavaScript regular expression assertions such as ^, $, \b, and \B to match positions without consuming characters.
What it is
An assertion is a zero-width part of a regular expression. It does not match a character; it checks whether the current position has a property. Think of the regex engine as a cursor moving through a string. Assertions ask questions like "Am I at the start of the string?" or "Is there a word boundary here?" The most common position assertions are ^ for start, $ for end, \b for a word boundary, and \B for a non-word boundary. Related terms include lookaheads and lookbehinds, which are also assertions but test surrounding text.
Why it matters
- They prevent accidental substring matches, such as matching
catinsidecategory. - They make validation clearer by anchoring patterns to line starts or ends.
- They help search and replace only whole words or tokens.
- They reduce the need for extra JavaScript checks after matching.
- They improve readability when the intent is "match this position," not "match this character."
Syntax or steps
The smallest useful pattern is /\bword\b/. Place \b before the word to require a transition from a non-word position to a word character, and after the word to require a transition from a word character to a non-word position. For line anchors, use /^start/ and /end$/. Add the m flag when you want ^ and $ to match the start and end of each line instead of only the whole string.
Example
const text = "The cat sat on the mat.\ncategory and concatenate.";
const wholeCat = /\bcat\b/g;
const catAnywhere = /cat/g;
const startsWithThe = /^The/m;
const endsWithMat = /mat\.$/m;
console.log(text.match(wholeCat)); // ["cat"]
console.log(text.match(catAnywhere)); // ["cat", "cat", "cat"]
console.log(startsWithThe.test(text)); // true
console.log(endsWithMat.test(text)); // true
Part by part: \bcat\b matches only the standalone word cat. It does not match category because the letter e follows cat, so there is no word boundary after it. It also does not match concatenate because c precedes the cat substring. The pattern /^The/m checks whether a line begins with The. The pattern /mat\.$/m checks whether a line ends with mat.; the backslash escapes the dot so it means a literal period.
Common mistakes
- Forgetting that
\bis zero-width. It matches a position, not a space. If you need actual whitespace, use\s. - Using
^and$on multiline text without themflag. Withoutm, they anchor to the whole string, not each line. - Assuming
\btreats hyphens or punctuation as part of a word. In JavaScript, word characters are letters, digits, and underscore, sofoo-barcontains boundaries aroundfooandbar. - Confusing
\bwith\B.\Bmatches positions that are not word boundaries, which is useful for matching inside words but easy to misuse.
When to use it
Use simple position assertions when the rule is about standard word boundaries or line edges. Use lookarounds when you need custom conditions around a match.
| Pattern | Use when | Example |
|---|---|---|
\b | You need a standard word boundary. | /\bcat\b/ |
^ and $ | You need to anchor to the start or end of a string or line. | /^TODO/m |
\B | You need to match only inside a word. | /cat\B/ |
| Lookahead | You need a custom "followed by" condition. | /cat(?=[^a-z])/ |
Practice
- Guided exercise: Write a regex that matches the whole word
runin"running run runner". Expected result:["run"]. - Challenge: Write a regex that matches lines starting with
TODOand ending with!. Hint: use/^TODO.*!$/m.
Quick check
Question: Why does /\bcat\b/ not match category? Answer: After cat, the next character is e, which is a word character, so there is no word boundary at that position.
Summary
Assertions match positions, not characters. Use \b for whole-word matching, ^ and $ for line or string edges, and the m flag when working with multiline text. They make JavaScript regular expressions more precise and easier to reason about.