JavaScript RegExp ^ Start Anchor

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

What Is the ^ Start Anchor?

^ 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)
UsePrefer
String / prefix start^pattern
Each line start^pattern + m
Full-string match^…$ with $

How to Use ^

Put ^ at the front of the pattern so matching can begin only at a start position.

  1. Write the text or tokens that must appear at the start (e.g. world, https?, #).
  2. Prefix the pattern with ^ — e.g. /^world/.
  3. Add m when every line should have its own start (multi-line text).
  4. Add $ when nothing may follow after the pattern (full-string validation).
PieceRequired?Role
^YesStart-position assertion
Pattern after ^YesWhat must appear at that start
m flagOptionalTreat each line as a new start
$OptionalAlso 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

Cheat Sheet

Keep these facts handy while anchoring patterns to the start.

TopicDetail
MeaningStart of string (or line with m)
WidthZero-width — does not consume characters
Prefix check/^hello/
Per-line start/^#/m
Full match/^\d+$/
Not the same as[^…] (negated character class)
StandardES3+ (universal)

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 */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Match a Prefix with ^

Check whether a string starts with world.

JavaScript
const phrases = ["world peace", "hello world", "world"];

phrases.forEach((s) => {
  console.log(s, /^world/.test(s));
});
Try It Yourself

How It Works

  1. Anchor first. ^ requires the match to begin at index 0.
  2. Prefix succeeds. "world peace" and "world" both start with world.
  3. Mid-string fails. "hello world" contains world, but not at the start → false.
  4. No scanning past ^. Unlike a plain /world/, the engine will not look later in the string.

Example 2 — Start of Each Line with ^ and m

Find lines that begin with start in multi-line text.

JavaScript
const text = "start here\nnot start\nstart again";

console.log(/^start/.test(text));    // true (string start)
console.log(/^start/m.test(text));   // true (any line start)
console.log(text.match(/^start/gm)); // ["start", "start"]
Try It Yourself

How It Works

  1. Default ^. Without m, only the very first character of the string counts as start.
  2. Add m. Each newline creates another start position for ^.
  3. Collect with g. /^start/gm finds both line-1 and line-3 matches.
  4. Middle line skips. "not start" has start later, so its line start still fails.

Example 3 — Full-String Match with ^ and $

Ensure an entire value contains only digits — nothing before or after.

JavaScript
const rule = /^\d+$/;

console.log(rule.test("42"));     // true
console.log(rule.test("42abc"));  // false — trailing text
console.log(rule.test("abc42"));  // false — not at start
Try It Yourself

How It Works

  1. Pin the start. ^ requires digits to begin at index 0.
  2. Pin the end. $ requires the match to reach the end of the string.
  3. Partial fails. "42abc" and "abc42" both leave leftover characters.
  4. 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
^ Excellent

Bottom line: Safe everywhere. Use ^ whenever a match must begin at the start of the string or line.

Performance Notes

Pattern styleCostBest for
^prefixVery cheapPrefix checks, early fail
^…$CheapWhole-field validation
^…/gm on huge textLinear in linesLog / 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.

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.

Next: {X} exact quantifier

Learn how to require a token to repeat exactly X times.

{X} tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful