SyntaxStudy
Sign Up
JavaScript Intermediate 4 min read

Regex Anchors

Anchors

Anchors match positions rather than characters. ^ matches the start of a string; $ matches the end. \b matches word boundaries.

Example
/^hello/     // Must start with "hello"
/world$/     // Must end with "world"
/^hello world$/ // Exact full match

// Word boundary
/\bcat\b/   // Matches "cat" but not "catch" or "scat"
/\bcat\b/.test("the cat sat");  // true
/\bcat\b/.test("catch");        // false

// Multiline mode changes ^ and $ behavior
/^\d+$/m    // ^ and $ match line starts/ends
Pro Tip

Use ^ and $ to validate entire strings — without them, the pattern can match anywhere inside the string.