Level 1: Foundations · Lesson 7 of 30
Anchors ^ $
Anchors match a position, not a character. ^ pins a match to the start and $ to the end of the text, or of each line in multiline mode.
Most regex tokens consume characters. Anchors are different: they match a position between characters and consume nothing. The two basic anchors are the caret ^, which means the start, and the dollar sign $, which means the end.
Without any flags, ^ matches only at the very start of the whole text and $ only at the very end. The pattern ^\d+ finds digits only if the text begins with them. On 42 is the answer, not 7 it matches 42 and ignores 7. Likewise end$ matches only when the text finishes with the letters end.
Using both anchors together turns a search into a full check. ^\d+$ succeeds only if the entire text is made of digits. This is the standard way to validate input: ^[A-Z]{2}\d{4}$ means the text must be exactly two capital letters followed by four digits, with nothing before or after.
Multiline mode changes the meaning. With the m flag in JavaScript, re.MULTILINE in Python, or the m modifier in PHP, ^ and $ also match at the start and end of every line. That lets ^\w+ find the first word of each line in a block of text. Java and C# use a multiline option too, and Go and Rust use the inline flag (?m).
Ruby is the exception to remember. In Ruby, ^ and $ always match at line starts and ends, even without a flag. To anchor to the whole string in Ruby you use \A and \z. Python, PHP, Java, C#, Go and Rust also support \A and \z (Python spells the end anchor \Z), but JavaScript does not have them.
A common beginner bug is forgetting anchors in validation. Checking a phone number with \d{10} passes the text abc12345678901xyz, because the search finds ten digits somewhere inside. Adding ^ and $ forces the whole input to match. Some APIs anchor for you: Java's matches() and Python's re.fullmatch() require the full string to match.
Inside a character set the caret means something else: [^abc] is a negated set. Outside brackets, at the start of a pattern, it is the start anchor. The dollar sign only means end when it is outside brackets as well.
Live tester
3 matches (JavaScript engine)
alpha one beta two gamma three
Examples
Numbers
/^\d+/g42 is the answer, not 7
Matches: “42”
Only the number at the start of the text matches; 7 is ignored.
Text
/end$/gsend it to the end
Matches: “end”
Only the end at the very end of the text matches, not the letters inside send.
Codes
/^[A-Z]+-\d+$/gmAB-12 note AB-13 CD-99
Matches: “AB-12”, “CD-99”
With the m flag each line is checked on its own, so the line with a note prefix fails.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match lines that contain only digits. Use the multiline flag.
123 45a 6789 x99
Task 2: Match the first word of every line.
apple pie banana split cherry tart
Task 3: Match the last word of every line.
red car blue bike green bus