^ — 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,
^bagainsta\nb. Seven engines say no; Ruby saysb. 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 passesmas a flag argument and Node then answersbtoo. So the feature is universal; only the spelling differs. - Rows 5 and 6,
\A. The anchor that means "start of subject and nothing else" answersnofora\nbeverywhere it exists — and JavaScript does not have it. Node'snoin row 5 is not agreement: row 6 shows\Abmatching the textAbinxAb, because without theuflag\Ais an identity escape for the letterA. 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¶
$is not the end of the string — the same story at the other end, with the payload that gets through.- Flags, and where you can turn one on —
(?m), themflag, and Ruby's collision of letters.
See also¶
$·\A,\z,\Z·\b·\G— the other anchors.- Character class — the two other meanings of
^. - Validating input · Multiline text and logs — where the choice of anchor decides whether the code is correct.