JavaScript RegExp ^ Quantifier

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Start anchor

What You’ll Learn

The ^ quantifier is a start anchor. It matches a position at the beginning of the input—not a character—so you can require prefixes, validate whole strings with ^…$, and parse line-by-line text when combined with the m flag.

01

^ anchor

String start

02

Zero-width

No characters

03

^ + m

Line start

04

Prefix

hello, http

05

Not [^]

Class negation

06

Pair $

Full match

Introduction

Many tasks care about where matching begins. Does a URL start with https? Does a config line begin with #? Must a username start with a letter? The ^ anchor answers those questions by pinning the match to the start of the string or line.

Without ^, the pattern /world/ matches "hello world" anywhere the letters appear. Adding ^ gives /^world/, which matches only when world is at the very beginning—a much stricter and often more useful rule.

Understanding the ^ Start Anchor

Place ^ at the start of a pattern (or after an opening parenthesis in some constructs). It asserts that the current match position is at the beginning of the input. With the m multiline flag, it also matches immediately after each newline.

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

/^world/.test("world peace");  // true
/^world/.test("hello world");  // false
/^world/.test("world");        // true

^ is zero-width: it checks a boundary without consuming characters. The matched substring is still the literal text that follows—such as "world" in "world peace".

💡
Beginner Tip

Do not confuse start-anchor ^ with negation inside a character class: [^abc] means “not a, b, or c”—a completely different use of the caret symbol.

How to Use the ^ Anchor

Put ^ before the text or groups you want to require at the start, or pair it with $ for full-string validation:

JavaScript
/^hello/;                          // starts with hello
/^https?/;                         // URL prefix
/^#.+$/m;                          // comment lines
/^[A-Z]/;                          // starts with capital
/^[-+]?\d+$/;                      // signed integer

"  hello".match(/^\s*hello/);      // trim-aware start

Use test() for yes/no checks, match() with gm to collect line-start matches, and ^…$ for strict whole-value validation.

📝 Syntax

JavaScript
^pattern            // must start with pattern
^https?://           // URL at string start
^#                   // line starts with #
^.+$                 // non-empty whole string
^[A-Za-z]            // first char is a letter
^text                // with m: each line start

Common patterns

  • /^https?:\/\// — string begins with a web URL scheme.
  • /^#.+$/gm — every comment line in config text.
  • /^\d+$/ — entire value is digits only.
  • /^[a-z]/i — starts with a letter (any case).
  • /^ERROR/m — line begins with ERROR in logs.

⚡ Quick Reference

GoalPattern / API
Starts with text/^text/
Line starts with text/^text/m
Full string match/^pattern$/
URL at beginning/^https?:/
End anchor pair$ — see $ tutorial
Not negation in class[^abc] — different meaning

📋 ^ Start vs $ End Anchor

Pin matches to the beginning or end of a string or line.

Start
^world

Must begin with world

End
world$

Must finish with world

Full
^world$

Entire string is exactly world

No anchor
/world/

Matches anywhere in the string

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover basic prefix matching, multiline line starts, URL prefixes, comment lines, and full-string validation with ^ and $.

📚 Getting Started

Require text at the beginning of a string.

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));
});
// world peace true
// hello world false
// world       true
Try It Yourself

How It Works

^ forces the match to begin at the first character. "hello world" contains world but not at the start, so the test fails.

📈 Practical Patterns

Line starts, URLs, and validation.

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 (first line only)
console.log(/^start/m.test(text));   // true (lines 1 and 3)

console.log(text.match(/^start/gm)); // ["start", "start"]
Try It Yourself

How It Works

Without m, ^ only checks the string beginning. With m, each newline creates a new start position. Add g to collect every matching line start.

Example 3 — URL Must Start with http or https

Validate web links at the beginning of a string.

JavaScript
const links = [
  "https://example.com",
  "http://example.com",
  "ftp://example.com",
  "visit https://example.com"
];

links.forEach((url) => {
  console.log(url, /^https?:\/\//.test(url));
});
// https://example.com        true
// http://example.com         true
// ftp://example.com          false
// visit https://example.com  false
Try It Yourself

How It Works

^ ensures the scheme is at the very start. A URL embedded in prose fails because the string begins with visit, not http.

Example 4 — Find Comment Lines Starting with #

Extract every line that begins with a hash in config text.

JavaScript
const config = "host=localhost\n# comment\nport=3000\n# another";

const comments = config.match(/^#.+$/gm);
console.log(comments);        // ["# comment", "# another"]
console.log(comments.length); // 2
Try It Yourself

How It Works

^# with m matches # at the start of any line. $ bounds each match to one line. This pattern is a classic multiline use case.

Example 5 — Full-String Match with ^ and $

Ensure an entire value contains only digits.

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

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

How It Works

^ pins the match to the start; $ pins it to the end. Together they reject partial or embedded matches. See also the $ end anchor page.

🚀 Common Use Cases

  • Prefix checks — URLs, protocol schemes, or required leading text.
  • Input validation — whole-field rules with ^…$.
  • Log parsing — lines starting with ERROR, WARN, or timestamps.
  • Config files — comment lines, section headers, or key prefixes.
  • Markdown/headings — lines starting with # for titles.
  • Capitalization rules — names or sentences that must start with a letter.

Important Considerations

  • Multiline text — add m when ^ should apply per line, not only at string start.
  • Not negation[^abc] inside a class is negation, not a start anchor.
  • Leading whitespace — spaces before text prevent ^ from matching where you expect; trim or use ^\s* if needed.
  • Not a character^ is an assertion; it does not match the first letter itself.
  • Partial vs full match — without $, /^pattern/ still allows extra text at the end.
  • Windows line endings\r\n may affect line-start matching; normalize when parsing cross-platform files.

🧠 How the ^ Anchor Works

1

Reach start position

The engine moves to the beginning of the string—or after \n when m is set.

Anchor
2

Match following part

Literal text, classes, or groups after ^ are matched normally.

Pattern
3

Continue or finish

Later anchors like $ may require the match to reach the string or line end.

Bound
4

Collect with g

Use gm to find every line that starts with your prefix pattern.

📝 Notes

  • ^ pairs naturally with $ for whole-string or whole-line validation.
  • See the m modifier for per-line ^ behavior.
  • Previous topic: * zero-or-more quantifier.
  • End anchor counterpart: $ end quantifier.
  • \b word boundaries differ from ^; see \b.
  • Next topic: {X} exact-count quantifier.

Browser & Runtime Support

The ^ start anchor is part of core JavaScript regular expression syntax.

Baseline · ES3+

RegExp ^ anchor

Supported in every browser and Node.js version. Combine with m for line-by-line start matching.

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 ^ when a pattern must begin at the start of a string or line.

Conclusion

The ^ quantifier is the start anchor: it ensures your pattern begins where you expect—at the head of a string, a URL, or (with m) each line of multi-line text. It is zero-width, precise, and essential for prefix checks and strict validation.

Add m for multi-line input, pair ^ with $ when the entire value must match, and remember that [^…] negation inside classes is unrelated. Next, learn the {X} exact-count quantifier.

💡 Best Practices

✅ Do

  • Use ^ when prefix position matters (URLs, keys, headers)
  • Add m for multi-line strings that need per-line starts
  • Combine ^ and $ for strict full-value validation
  • Use gm to collect every line-start match
  • Normalize \r\n when parsing cross-platform text files

❌ Don’t

  • Confuse start-anchor ^ with [^abc] class negation
  • Forget m when matching line starts in logs or configs
  • Assume ^ consumes the first character
  • Use /^pattern/ alone when the whole string must match
  • Mix up ^ start anchor with \b word boundary

Key Takeaways

Knowledge Unlocked

Five things to remember about the RegExp ^ anchor

Match prefixes and start-of-string patterns with confidence.

5
Core concepts
📈 02

Zero-width

No chars eaten.

Concept
📄 03

^ + m

Line starts.

Multiline
🔒 04

^ $

Full match.

Validate
🚫 05

Not [^]

Class only.

Pitfall

❓ Frequently Asked Questions

In a pattern, ^ is a zero-width start anchor. It matches a position at the beginning of the input string, or—when the m (multiline) flag is set—immediately after each newline character. It does not consume any characters.
No. Outside a character class, ^ is a start anchor. Inside [^abc], the caret means negation (not a, b, or c). Context determines the meaning.
Use ^ with m when your text contains newlines and you want to match the start of each line—not only the very first character of the entire string. Example: /^#/m matches lines that begin with #.
^ matches the start of the string or line (with m). \b matches a word boundary. /^word/ requires word at the very start; /\bword/ finds word after a non-word character too.
Use ^ at the start and $ at the end to require the entire string to match the pattern. Example: /^\d+$/ validates that the whole value is digits only.
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.
Did you know?

The same ^ character means two different things in regex: a start anchor outside character classes, and negation inside […]. When reading patterns, check whether the caret is at the pattern start or immediately after [—that single clue tells you which meaning applies.

Continue to {X} exact-count quantifier

Learn how {X} matches an exact number of repetitions.

{X} quantifier 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.

6 people found this page helpful