Most regex surprises are not about characters. The pattern finds the right digits and the right word, but in the wrong places. Sometimes that is the start of the text when you wanted the start of a line. Sometimes it is buried inside a longer word when you wanted a standalone token. The missing piece is position. Anchors and boundaries test where a match sits instead of what it contains. Once they click, a family of "why did it match that" bugs goes away.

This guide explains ^, $, \b and \B with small tests you can reproduce in the free Regex Tester on Ovatly, including the one flag that changes two of them.

Contents

  • Quick answer
  • Positions, not characters
  • ^ and $: the edges of the text
  • The m flag: the edges of every line
  • \b: the edge of a word
  • \B: everywhere \b is not
  • How to test anchors, step by step
  • Worked example: one pattern, two match lists
  • Limitations
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Anchors match positions, not characters, so they add nothing to the matched text. ^ matches at the start of the text and $ at the end; with the m flag they match at the start and end of every line instead. \b matches at the edge of a word, where a word character meets a non-word character or the edge of the text. \B matches at every position where \b does not. To see any of this, paste a small sample into the Regex Tester and run the pattern with the g flag. Then compare the match list with and without m.

Positions, not characters

An ordinary pattern piece consumes text: \d eats one digit, cat eats three letters, and the match is exactly what was eaten. An anchor eats nothing: it is a test that passes or fails at a position between characters, or at either end of the text. That is why a match for ^\d+ is reported as just the digits: the ^ required "at the start" and matched zero characters itself.

In the tester, each match shows its text, its index and its groups, and the index is where the position test passed. When an anchored pattern matches less than expected, the position was usually wrong, not the characters. The fix is so often a flag, not a rewrite.

^ and $: the edges of the text

By default, ^ passes only at position 0, before the first character, and $ passes only after the last character. A pattern like ^\d+ therefore finds digits that open the text, and \d+$ finds digits that close it. On a multi-line text both stay attached to the whole text, not to any line inside it. That is correct for validating a complete field, where the whole string must conform. It is wrong for scanning logs and lists, which is where the next section's flag comes in.

The m flag: the edges of every line

The m flag, short for multiline, redefines the two anchors: ^ now also passes after every line break, and $ also passes before every line break. Nothing else changes. The same ^\d+ that found one match in a three-line text finds two with m added, because two of the lines start with digits. The worked example below runs exactly this comparison: same pattern, same text, one flag different, two visibly different match lists.

\b: the edge of a word

\b passes at a position where one side is a word character and the other side is not. That means between a letter and a space, or between a digit and punctuation. It also means at the edges of the text beside a word character. Wrapping a pattern in \b on both sides asks for a whole token rather than a substring. The classic demonstration is \b\d+\b on "room 45b and room 45", which reports exactly one match, the final 45 at index 18. The digits inside 45b are skipped. After the 5 comes a b, and both are word characters, so the closing \b has no boundary to pass at.

\B: everywhere \b is not

\B is the exact complement: it passes at every position where \b fails, meaning inside a word and between two non-word characters. A pattern like a\B makes it concrete: it asks for an a that is not at the end of a word. On "apple banana" with the g flag, the tester reports three matches. They are the a at index 0 of apple, and the a at indexes 7 and 9 inside banana. The final a of banana, at index 11, is not reported. After it comes the end of the text, which is a boundary, and \B refuses boundaries. Reach for \B when you want a match strictly inside a token.

How to test anchors, step by step

  1. Open the Regex Tester.
  2. Type the anchored pattern, without slashes, in the Pattern field: ^\d+
  3. Put g in the Flags field, so every match is listed.
  4. Paste a small multi-line sample, such as the one below.
  5. Read the match list, noting each match's text and index.
  6. Add m to the Flags field so it reads gm, and compare the new match list against the old one.

An invalid pattern error usually means an unclosed bracket or a trailing backslash; the tester explains it in plain language.

Worked example: one pattern, two match lists

Use this three-line sample:

45 apples
box 45
99 red balloons

Run ^\d+ with flags g. The tester reports one match: 45 at index 0. The digits opening the third line are ignored: without m, ^ recognises only the start of the whole text. With flags gm instead, the list grows to two matches: 45 at index 0 and 99 at index 17, the start of the third line.

The mirror image works at line ends. \d+$ with flags gm reports one match, the 45 at index 14 that ends the second line. The same pattern with g alone reports no matches at all. The text ends with "balloons", not with digits. Four runs, and the behaviour of all three is visible in the lists.

Limitations

Anchors test position only. They cannot express ideas like "the second line" or "after the third word", so some jobs still need a character pattern around the anchor. The tester runs JavaScript's RegExp engine. Its rules differ slightly from the PCRE, Python and grep flavors, so a pattern tuned here can behave a little differently elsewhere. Results also stop at 500 matches, which matters for anchor-heavy patterns over long texts. A pattern that can match an empty string fills the list quickly. The tester advances such patterns safely, but the cap still applies.

Common mistakes

  • Expecting ^ to mean line start without m. It means text start. Most anchor complaints are this one missing flag.
  • Thinking \b matches a space. It matches no character at all; it passes at the position next to the space. That is why anchored matches contain only real characters.
  • Typing anchors or flags into the wrong field. Anchors belong in the Pattern field; the Flags field takes only the letters g, i, m, s, u and y.
  • Forgetting the g flag while testing. Without it, only the first match is reported, so a boundary pattern looks broken when it is merely untested past its first success.
  • Wrapping a pattern in \b where the edge is punctuation. \b needs a word character on one side. A token starting with a symbol has no boundary before it, and \B or a plainer pattern is the fix.

FAQs

Do anchors match characters in the text? No. Anchors match positions: the start or end of the text, the start or end of a line, or the edge of a word. They add nothing to the matched text, which is why a match for ^\d+ is just the digits.

Why did ^ match only once in my multi-line text? Without the m flag, ^ means the start of the whole text. Add m in the Flags field and it also matches at the start of every line. $ changes the same way, from end of text to end of each line.

What is the difference between \b and \B? \b matches a position where a word character sits next to a non-word character or the edge of the text. \B matches every position where \b does not: inside a word, or between two non-word characters.

Why did \b\d+\b skip the number in 45b? Between the 5 and the b there is no boundary, because both are word characters. The trailing \b cannot match there, so the whole candidate fails and only the standalone 45 is reported.

Do anchors go in the Flags field? No. Anchors are part of the pattern itself, typed in the Pattern field without slashes. Only the letters g, i, m, s, u and y go in the Flags field.

Conclusion

Once anchors are understood as position tests, patterns stop being lucky guesses. ^ and $ pin a match to the edges of the text or, with m, of each line. \b and \B pin it to the edges of a word or strictly inside one. The companion guide How to Extract All Email Addresses from Text with Regex puts them to work in a real extraction job, and more tool walkthroughs live in the Developer Tools guides archive.