Skip to content

Chapter 3 of 8

Anchors and Boundaries

Anchors do not consume characters; they assert something about the current position in the input. The caret ^ matches the start of the string by default, and with the m flag it also matches the start of each line following a newline. The dollar sign \( matches the end of the string, and with the m flag it also matches just before any newline. Be aware that in JavaScript, even without the m flag, \) will still match before a final newline in some engines, which is a subtle source of off-by-one bugs. For unambiguous absolute positions, PCRE and Python provide \A for the strict start of input and \z for the strict end, with \Z matching end-of-string or just before a final newline.

The word boundary \b is one of the most useful zero-width assertions. It matches the position between a word character (matched by \w) and a non-word character, or at the start or end of the string. The pattern \bcat\b therefore matches the standalone word cat but not the cat inside concatenate. The negation \B matches anywhere that is not a word boundary, which lets you write \Bcat\B to find the substring cat only when it is embedded inside a longer word. Word boundaries are invaluable when searching for keywords in text, because they let you avoid partial matches without listing all possible surrounding characters.

Anchors behave differently inside character classes, and this is a frequent source of confusion. Outside a class, ^ is the start anchor; inside [...], when it is the first character, it negates the class so [^a-z] matches anything that is not a lowercase ASCII letter. Outside a class, \( is the end anchor; inside [\^\)] it is a literal caret or dollar sign. A common idiom ^|$ matches the start or end of a line in multiline mode, but it is sometimes mistakenly written as a character class, which would match a literal caret or dollar sign instead. When in doubt, prefer explicit anchors like \A and \z when you want true start-of-string and end-of-string semantics that no flag can change.

All chapters
  1. 1Character Matching: Metacharacters, Shorthands, and Classes
  2. 2Quantifiers: Greedy, Lazy, Possessive, and Exact
  3. 3Anchors and Boundaries
  4. 4Groups, Captures, and Backreferences
  5. 5Lookaround Assertions
  6. 6Flags, Modes, and Engine Features
  7. 7Practical Recipes for Validation and Extraction
  8. 8Performance and Engine Internals

Drill it

Reading is not remembering. These come from the Regex Patterns Every Developer Should Know deck:

Q

What does the regex metacharacter <code>.</code> match by default?

A single character of any kind except the newline character (\n). To make it also match newlines, use the s (dotall / single-line) flag, e.g. /foo.bar/s in Java...

Q

Which character class matches any single digit in most regex flavors?

[0-9] or the shorthand \d. \d is equivalent to [0-9] in ASCII; with Unicode (u flag in JS, default in Python 3 str) it matches any Unicode decimal digit, e.g. A...

Q

What does the shorthand <code>\w</code> match?

Any word character. In ASCII mode this is [A-Za-z0-9_]; in Unicode mode it also matches letters and digits from other scripts, and the underscore.

Q

What does the shorthand <code>\s</code> match?

Any whitespace character: space, tab (\t), newline (\n), carriage return (\r), form feed (\f), and vertical tab (\v). In Unicode mode it can also match rare whi...