Regex Anchors Tester (^, $, \b, \B)
Regex anchors are zero-width assertions — they match positions, not characters. ^ = start of line. $ = end of line. \b = word boundary. The behavior changes with the /m (multiline) flag. This tester visualizes every anchor position in your input.
How to use the Regex Anchors Tester (^, $, \b, \B)
Pick an anchor. Toggle multiline mode. The output highlights every position where the anchor matches.
Anchors match positions, not characters
An anchor is a zero-width assertion: it matches a position between characters rather than consuming a character. ^ asserts the start of the string (or of a line with the m flag) and $ the end. \b sits on a word boundary — the seam between a \w character and a \W character or string edge — while \B matches everywhere that is not such a seam.
Because they consume nothing, anchors are how you say "this must be the whole value" with ^...$, or "this word, not a fragment of a longer one" with \bcat\b so it skips category. The multiline flag is the usual source of confusion, and seeing every match position highlighted makes its effect obvious.
Common use cases
- Whole-string validation — wrap a pattern in
^...$so a partial match cannot sneak through. - Whole-word search — use
\bto matchcatwithout matchingconcatenate. - Multiline log scanning — with the
mflag,^ERRORfinds the start of every failing line. - Debugging stray matches — see exactly why a pattern fires mid-string when you expected the start.
- Trimming and splitting — anchor the edges before stripping leading or trailing tokens.
Frequently asked questions
What is the difference between <code>^</code> and <code>\A</code>?
^ matches the start of every line when the multiline flag is set, whereas \A always matches only the absolute start of the string. JavaScript has no \A; PCRE, Python, Java, and .NET do.Why does <code>\b</code> behave oddly with accented or non-Latin letters?
\w covers only ASCII, so boundaries can fall in surprising places inside words with combining accents or non-Latin scripts. Switch to Unicode-aware matching (\p{L} with the u flag) or a Unicode regex library to fix it.Does <code>$</code> match before a trailing newline?
$ can match just before a final \n. When you need the true end of input, use \z, or \Z for "end, optionally before a final newline".What exactly does the multiline flag change?
^ and $, which then match at line breaks. It does not change . — making the dot match newlines is the separate dotall (s) flag.