Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท RegExp

Regular expression anchors and boundaries

Reference for JavaScript regular expression anchors and word boundaries: ^, $, \b and \B, how the m flag changes them, and common validation mistakes.

Anchors and boundaries do not match characters. They match a position in the string and consume nothing, which is why they are called zero-width assertions. ^ is the start, $ is the end, \b is the edge of a word and \B is any position that is not.

Anchors are what turn "contains a match" into "is a match". /\d+/.test("abc 42") is true because digits appear somewhere; /^\d+$/.test("abc 42") is false because the whole string must be digits.

JavaScript
/^start/      // at the beginning
/end$/        // at the end
/\bcat\b/     // the whole word "cat"
/\Bcat/       // "cat" inside a longer word

Try it

JS Whole-string checks and word boundaries

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS The m flag and lines

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
^     // start of input (or of a line with the m flag)
$     // end of input (or of a line with the m flag)
\b    // word boundary
\B    // not a word boundary
Anchor Matches
^ The position at the start of the string. With the m flag, also right after every line terminator.
$ The position at the end of the string. With the m flag, also right before every line terminator.
\b A position where a word character (\w) is next to a non-word character, or to the start or end of the string.
\B Any position that is not a \b position, including between two word characters and between two non-word characters.

Usage notes

  • Inside [...] they mean something else. A leading ^ negates the set and \b is the backspace character. See character classes.
  • $ is strict. Unlike some other languages, $ without the m flag matches only at the very end of the string, not before a final newline. /^abc$/.test("abc\n") is false. Trim input first if that matters.
  • ^ and $ bind to alternatives separately. /^cat|dog$/ means "starts with cat, or ends with dog". Group it to anchor both: /^(?:cat|dog)$/.
  • \b uses \w, which is ASCII. Between รฉ and a space there is no boundary according to \b, because รฉ is not a word character, so /\bcafรฉ\b/ fails on "cafรฉ au lait". For non-English text use lookarounds such as (?<!\p{L}) and (?!\p{L}) with the u flag (see lookaround).
  • \b at the string edges counts only when the first or last character is a word character.
  • There is no \A or \z. Use ^ and $ without the m flag.
  • The y (sticky) flag is a different kind of anchor: it forces a match to start exactly at lastIndex (see lastIndex).
  • ^ and $ cannot be repeated. /^*/ is a syntax error; to require several lines in a row, group the line pattern first.
  • Validation tip: anchors prevent partial matches, but they do not prove an input is sensible. Keep patterns simple and validate the meaning separately.

Browser support

^, $, \b and \B work in every JavaScript engine.