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
Fundamentals
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.
Concept
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.
^ 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.
Usage
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.
Foundation
📝 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.
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
Hands-On
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
^# 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
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 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 ^ when a pattern must begin at the start of a string or line.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about the RegExp ^ anchor
Match prefixes and start-of-string patterns with confidence.
5
Core concepts
🔢01
^ anchor
Start position.
Syntax
📈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.