The match for the empty line followed by the new line fails for the regex ^$^J but succeeds for the regex ^^J. What is wrong with the former regex?
$ in a regexp usually matches the empty string at an end of line, but in reality this is only true if the $ appears at specific places in the regexp (e.g. at the end of the regexp, or at the end of a sub-group, IIRC). If the $ appears "in the middle" it just matches the $ character. Same applies to ^. E.g. (string-match "a^b$c" "1a^b$c2") returns 1.
C-hig (emacs) Regexps documents this behaviour:
‘^’
is a special character that matches the empty string, but only at
the beginning of a line in the text being matched. Otherwise it
fails to match anything. Thus, ‘^foo’ matches a ‘foo’ that occurs
at the beginning of a line.
For historical compatibility reasons, ‘^’ can be used with this
meaning only at the beginning of the regular expression, or after
‘\(’ or ‘\|’.
‘$’
is similar to ‘^’ but matches only at the end of a line. Thus,
‘x+$’ matches a string of one ‘x’ or more at the end of a line.
For historical compatibility reasons, ‘$’ can be used with this
meaning only at the end of the regular expression, or before ‘\)’
or ‘\|’.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With