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.
/^start/ // at the beginning /end$/ // at the end /\bcat\b/ // the whole word "cat" /\Bcat/ // "cat" inside a longer word
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
^ // 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\bis the backspace character. See character classes. $is strict. Unlike some other languages,$without themflag matches only at the very end of the string, not before a final newline./^abc$/.test("abc\n")isfalse. 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)$/.\buses\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 theuflag (see lookaround).\bat the string edges counts only when the first or last character is a word character.- There is no
\Aor\z. Use^and$without themflag. - The
y(sticky) flag is a different kind of anchor: it forces a match to start exactly atlastIndex(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.
Related
- Regular expression flags โ the
mflag - Lookahead and lookbehind โ other zero-width assertions
- Character classes
- RegExp.prototype.test()
- Lesson: Regular Expressions