Skip to content

^ — start of the subject, or of a line, or "not"

One line: Outside a class ^ is an anchor that matches at the start of the subject — and at the start of every line in Ruby always, and elsewhere only under (?m) or an m flag; as the first character inside [...] it means "not these"; anywhere else inside a class it is a literal caret.

Level: reference · one construct, eight engines

What it means

^ consumes nothing. It succeeds at a position, and which positions depends on the engine and its flags:

Position Plain ^ Under multiline
Offset 0 yes, everywhere yes
Just after a \n in the middle Ruby: yes. Everyone else: no yes, everywhere
Inside [...] as first char negation, not an anchor —
Inside [...] elsewhere the character ^ —

The one engine where the plain form is already a line anchor is Ruby, and that is the row to remember from the table below.

Measured

Verified output of kw_caret_sh.sh — regenerated by tools/run_examples.py, never hand-typed.

construct                 python  perl  node  ruby  java  go  rust  fancy
start of subject          a       a     a     a     a     a   a     a
second line, no flag      no      no    no    b     no    no  no    no
second line, (?m)         b       b     -     b     b     b   b     b
second line, node m flag  no      no    b     b     no    no  no    no
\A instead                no      no    no    no    no    no  no    no
\A in JavaScript          no      no    Ab    no    no    no  no    no
mid-pattern ^             no      no    no    no    no    no  no    no
negation inside a class   b       b     b     b     b     b   b     b
literal inside a class    ^       ^     ^     ^     ^     ^   ^     ^

Reading the table

  • Row 2, ^b against a\nb. Seven engines say no; Ruby says b. Onigmo's ^ is a line anchor with no flag involved, and there is no flag to turn that off. A Ruby validator written ^\d+$ is a test on one line of the input, which is the whole of $ is not the end of the string.
  • Row 3, (?m)^b. The inline flag makes the other six agree with Ruby. JavaScript's cell is - because JavaScript has no inline flags at all — row 4 passes m as a flag argument and Node then answers b too. So the feature is universal; only the spelling differs.
  • Rows 5 and 6, \A. The anchor that means "start of subject and nothing else" answers no for a\nb everywhere it exists — and JavaScript does not have it. Node's no in row 5 is not agreement: row 6 shows \Ab matching the text Ab in xAb, because without the u flag \A is an identity escape for the letter A. The pattern compiles, and means something else.
  • Row 7, a^b. A caret that is not at the start is still an anchor, not a literal, in all eight — the pattern can never match. If you meant the character, escape it or put it in a class (row 9).
  • Rows 8 and 9. Inside brackets the caret has two jobs by position: first means negation, anywhere else means itself.

The trap

Two, and they point in opposite directions. In Ruby, ^ is a line anchor and people assume it is a string anchor. In JavaScript, people reach for \A from Perl or Python and get a pattern that matches the letter A. Neither produces an error; both produce a match result that looks plausible on test data.

For the start of the subject and nothing else, write \A where it exists (start and end of the subject) and, in JavaScript, ^ without the m flag — which is the one engine-specific rule worth memorising.

Where it is measured in depth

See also