Anchors ^ and $
^ matches the start of the string and $ the end, so ^abc$ matches only the exact string abc. With the m (multiline) flag they match at the start and end of each line instead. Anchors match positions, not characters, and consume nothing. \b is the related word-boundary anchor.
Syntax
/^pattern$/ /^pattern$/m (per line) Python and PCRE: \A and \Z for absolute start and endExamples
["cat", "concat", "cats"].map(s => /^cat$/.test(s))Result: [true, false, false]
"ERROR a\nINFO b\nERROR c".match(/^ERROR.*$/gm)Result: ["ERROR a", "ERROR c"]
With m, ^ and $ work per line.
"ERROR a\nINFO b".match(/^ERROR.*$/g)Result: null
Without m, $ means end of the whole input and the dot cannot cross the newline.
"price: 100 USD".replace(/\s*USD$/, "")Result: "price: 100"
Remove a suffix only when it is at the end.
Common mistakes
- Expecting ^ and $ to work per line without the m flag.
- Writing ^cat|dog$ when you mean ^(?:cat|dog)$; alternation binds loosest, so the anchors apply to only one side each.
- Using ^ inside brackets, where [^...] means a negated class, not start of string.
- Assuming $ ignores a trailing newline: in JavaScript it does not (abc\n fails ^abc$), while Python and PCRE let $ match before a final newline.
Practice Anchors ^ and $
These drills check your answer on live data, free, no account needed.
- Anchors: lines that start with ERRORA match anywhere counts, so anchor the pattern yourself. Match lines that begin with the word ERROR in capitals.
- Alternation with anchorsA match anywhere counts, so anchor it yourself. Match lines that are exactly yes or exactly no.
- Multiline flag: TODO linesEach test is a block of several lines. Capture the text after TODO: on every line that starts with TODO: (TODO, colon, space).
Questions
What does ^ mean in regex?
Outside brackets it anchors the match to the start of the string, or the start of a line with the m flag. Inside brackets as the first character it negates the class.
What is the difference between $ and \Z?
In Python, $ matches at the end of the string and also just before a trailing newline; \Z matches only at the absolute end. JavaScript has no \Z and its $ is already absolute without the m flag.
How do I match an entire line?
Use ^...$ with the m flag: /^ERROR.*$/gm returns every line that starts with ERROR. Add the s flag only if you want the dot to cross lines.
Related
- Word boundary \bWhat \b matches, how to find or replace whole words only, what \B does, and why \b fails next to punctuation, in string-built regexes and with Unicode text.
- Regex flagsWhat each JavaScript regex flag does (case-insensitive, global, multiline, dotAll, unicode, sticky), how to set them, and the lastIndex trap with g and test.
- Alternation |How the pipe works, why ^cat|dog$ does not do what it looks like, how to group alternatives, and why option order matters when one is a prefix of another.
- Escaping special charactersThe regex metacharacters that must be escaped, how to match a literal dot, dollar, plus or slash, what changes inside brackets, and how to escape user input.