Anchors
Anchors match positions rather than characters. ^ matches the start of a string; $ matches the end. \b matches word boundaries.
Anchors match positions rather than characters. ^ matches the start of a string; $ matches the end. \b matches word boundaries.
/^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
Use ^ and $ to validate entire strings — without them, the pattern can match anywhere inside the string.
More in JavaScript