Back to JavaScript Notes
Topic #346

Assertions

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 cat inside category.
  • 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 \b is zero-width. It matches a position, not a space. If you need actual whitespace, use \s.
  • Using ^ and $ on multiline text without the m flag. Without m, they anchor to the whole string, not each line.
  • Assuming \b treats hyphens or punctuation as part of a word. In JavaScript, word characters are letters, digits, and underscore, so foo-bar contains boundaries around foo and bar.
  • Confusing \b with \B. \B matches 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.

PatternUse whenExample
\bYou need a standard word boundary./\bcat\b/
^ and $You need to anchor to the start or end of a string or line./^TODO/m
\BYou need to match only inside a word./cat\B/
LookaheadYou need a custom "followed by" condition./cat(?=[^a-z])/

Practice

  1. Guided exercise: Write a regex that matches the whole word run in "running run runner". Expected result: ["run"].
  2. Challenge: Write a regex that matches lines starting with TODO and 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Assertions – FAQs

Quick answers about learning Assertions in JavaScript.

This free note from Coding Now Tech Institute explains Assertions in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Assertions, is 100% free with no signup required.
With focused practice, most students grasp Assertions in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now