By default, the caret ^ metacharacter matches the position before the first
character in the string.
Given the string "charsequence" applied
against the following patterns: /^char/ & /^sequence/, the engine will try to match as follows:
/^char/
charsequencecharsequencecharsequencecharsequencecharsequenceMatch Found
/^sequence/
charsequencecharsequenceMatch not Found
The same behaviour will be applied even if the string contains line terminators, such as \r?\n. Only the position at the start of the string will be matched.
For example:
/^/g
┊char\r\n
\r\n
sequence
However, if you need to match after every line terminator, you will have to set the multiline mode (//m, (?m)) within your pattern. By doing so, the caret ^ will match "the beginning of each line", which corresponds to the position at the beginning of the string and the positions immediately after1 the line terminators.
1 In some flavors (Java, PCRE, ...), ^ will not match after the line terminator, if the line terminator is the last in the string.
For example:
/^/gm
┊char\r\n
┊\r\n
┊sequence
Some of the regular expression engines that support Multiline modifier:
Pattern pattern = Pattern.compile("(?m)^abc");
Pattern pattern = Pattern.compile("^abc", Pattern.MULTILINE);
var abcRegex = new Regex("(?m)^abc");
var abdRegex = new Regex("^abc", RegexOptions.Multiline)
/(?m)^abc/
/^abc/m
Python 2 & 3 (built-in re module)
abc_regex = re.compile("(?m)^abc");
abc_regex = re.compile("^abc", re.MULTILINE);