The ^ anchor: start of string, or start of line
Outside a character class, ^ asserts that the current position is the start of the string, or the start of a line when the m flag is set. It matches a position, never a character.
At a glance
- Syntax
- ^
- Width
- zero, the position does not advance
- Without the m flag
- matches only at index 0
- With the m flag
- also matches directly after every \n, \r, U+2028 or U+2029
- Inside a character class
- [^abc] is negation, an unrelated meaning for the same character
- Simpler alternative
- String.prototype.startsWith, for a plain literal prefix
In detail
Because the anchor is zero width, it asserts something about a position without consuming anything there. That is easiest to see in a replace: swapping /^/ for a marker on "abc" produces ">abc", with the original string entirely intact. Add g and m and the same trick prefixes every line, which is how quoting and indenting are usually done.
With the m flag off, ^ matches at index zero and nowhere else, so /^b/ never matches "a\nb" no matter how the string is laid out. That is the correct default for validation and the wrong one for line processing, and the choice between them is the entire content of the m flag.
The same character does a completely different job inside brackets. As the first item in a character class it negates the class, and anywhere else in a class it is a literal caret. So [^a] means not an a, while [a^] means an a or a caret, and the difference is one position in the source.
An anchor on one end only is half a validator. /^\d+/ accepts the string "12abc", because it pins the start and then lets the rest of the input go unchecked. Anything being used as a yes or no answer about a whole value needs both ends anchored, or it is decorative.
Examples
"a\nb".match(/^b/); // null
"a\nb".match(/^b/m)[0]; // "b"
"abc".replace(/^/, ">"); // ">abc"
"a\nb".replace(/^/gm, "> "); // "> a\n> b"/^a/.test("abc"); // anchor, position 0
/[^a]/.test("b"); // negation, because ^ is first in the class
"a^b".match(/[a^]/g); // ["a", "^"], a literal caret elsewhere in a classThings that catch people out
- /^\d+/ is not a validator. It anchors the start and allows anything afterwards, so "12abc" passes. Without a $ as well, the check is cosmetic.
- Inside a character class the caret only negates in first position. [a^b] matches a literal caret and silently accepts input the author meant to reject.
- ^ is zero width, so a global replace against a pattern made only of anchors inserts text at every matching position instead of removing anything.
Try it: Regex Tester
Type a pattern and a test string, watch every match highlight as you go, read a plain English breakdown of each token, and check the numbered and named groups it captured.
Regex Tester
LOCAL · Browser-OnlyTest and debug regular expressions with live matching and highlights
Related Regex syntax
All regex syntax →The $ anchor: end of string, and the trailing newline question
Where $ matches in JavaScript, why it refuses to match before a trailing newline when Python and Ruby allow it, and how to write a literal dollar sign.
The m flag: multiline anchors
What multiline mode changes about ^ and $, which line terminators JavaScript counts, and why the m flag never makes the dot match a newline.
\A and \z in JavaScript: what to use instead
JavaScript has no \A, \z or \Z. Here is what those anchors silently turn into, and the absolute start and end assertions that survive the m flag.
Anchoring to a line without turning on the m flag
How lookaround assertions give you per line anchors at one position in a pattern while ^ and $ keep their whole string meaning everywhere else.
(...) capturing groups: numbering and what comes back
How capture groups are numbered, what a group that did not participate returns, and why quantifying a group throws away everything but the last pass.
(?:...): grouping without creating a capture
A non capturing group applies quantifiers and alternation without taking a number, which is what keeps $1 pointing at the same thing after an edit.
Tools for this
- Regex Tester Free online regex tester - test and debug regular expressions with live matching and highlights
- Regex Playground Regex playground with live match highlighting, named groups, find and replace, and shareable links