^ is a zero-width assertion: it matches a position at the start of the string (or each line with the m flag), not a character. Use it for prefixes, line starts, and full-string checks with $.
Remember
^pattern → must start at position 0
^pattern/m → start of each line (after \n)
^…$ → entire string must match
/^world/ → "world…" ✓ "hello world" ✗
[^abc] → NOT a start anchor (class negation)
Put ^ at the front of the pattern so matching can begin only at a start position.
Write the text or tokens that must appear at the start (e.g. world, https?, #).
Prefix the pattern with ^ — e.g. /^world/.
Add m when every line should have its own start (multi-line text).
Add $ when nothing may follow after the pattern (full-string validation).
Piece
Required?
Role
^
Yes
Start-position assertion
Pattern after ^
Yes
What must appear at that start
m flag
Optional
Treat each line as a new start
$
Optional
Also pin the end for a full match
Pseudocode
Pseudocode
pattern = ^ <rest>
positions = [0] // always: string start
if flag m is set:
also add index after every \n
for each startPos in positions:
if <rest> matches from startPos:
return match
return no match // ^ itself consumes nothing
Reference
Cheat Sheet
Keep these facts handy while anchoring patterns to the start.
Topic
Detail
Meaning
Start of string (or line with m)
Width
Zero-width — does not consume characters
Prefix check
/^hello/
Per-line start
/^#/m
Full match
/^\d+$/
Not the same as
[^…] (negated character class)
Standard
ES3+ (universal)
Try it
Live Preview
Pick a start-anchored pattern and a sample string to see whether test() passes.
Browser demo of the ^ start anchor — same idea as the examples below.
Result
pattern = /^world/
text = "world peace"
test() → true
/* ^ pins the match to the first character */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Pin the start.^ requires digits to begin at index 0.
Pin the end.$ requires the match to reach the end of the string.
Partial fails."42abc" and "abc42" both leave leftover characters.
Validation pattern.^…$ is the usual shape for whole-field checks. See also $.
Edge Cases & Pitfalls
Check these before relying on ^ in real projects.
trap
[^abc] is not a start anchor
Inside a character class, ^ means negation. Outside, it means start. Different contexts, different jobs.
multiline
Forgot the m flag
Without m, ^ only checks the string beginning — not each line. Add m for per-line starts.
spaces
Leading whitespace blocks prefixes
/^hello/ fails on " hello". Trim first, or allow spaces with /^\s*hello/.
partial
^ alone still allows a trailing suffix
/^42/ matches "42abc". Pair with $ when the whole string must match.
Compatibility
Browser & Runtime Support
The ^ start anchor is part of core JavaScript regex (ES3+). Multiline m behavior is equally universal.
✓ Baseline · ES3+
RegExp ^ start anchor
Supported in every modern and legacy browser, plus all Node.js versions. No polyfill needed.
99%Universal syntax
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
^Excellent
Bottom line: Safe everywhere. Use ^ whenever a match must begin at the start of the string or line.
Analysis
Performance Notes
Pattern style
Cost
Best for
^prefix
Very cheap
Prefix checks, early fail
^…$
Cheap
Whole-field validation
^…/gm on huge text
Linear in lines
Log / config line scans
Anchors help engines fail fast: if the start does not match, there is nothing to scan. Prefer ^ (and $) over unanchored patterns when you know the match must sit at an edge.
Remember
Key Takeaways
Role:^ matches a start position, not a character.
Multiline: add m so ^ applies to every line.
Full match: pair with $ as ^…$.
Trap:[^abc] is class negation, not a start anchor.
One line:^ pins the match to the start — add m for lines, and $ when the whole string must match.
Frequently Asked Questions
Outside a character class, ^ is a zero-width start anchor. It matches a position at the beginning of the string, or—with the m (multiline) flag—at the start of each line. It does not consume any characters.
No. Outside a class, ^ is a start anchor. Inside [^abc], the caret means negation (not a, b, or c). Context decides the meaning.
Use ^ with m when your text has newlines and you want to match the start of each line—not only the first character of the whole string. Example: /^#/m matches lines that begin with #.
^ matches the start of the string or line (with m). \b matches a word boundary anywhere. /^word/ requires word at the very start; /\bword/ can find word after punctuation too.
Use ^ at the start and $ at the end to require the entire string to match. Example: /^\d+$/ means the whole value is digits only—no extra text before or after.
The ^ anchor is core JavaScript regex syntax since ES3. It works in every browser and Node.js version. Multiline behavior with m is equally universal.