^
- matches beginning of string(
- starts capture group #1(.)
- matches any single character except a newline, save it in capture group #2(?1)
- recurse = replace this group with the entire regexp capture group #1\2
- matches the same thing as capture group #2. This requires the first and last characters of the string to match each other|
- creates an alternative.?
- optionally matches any one character that isn't a newline - This handles the end of the recursion, by matching an empty string (when the whole string is an even length) or a single character (when it's an odd length))
- ends capture group #1$
- matches end of string or before a newline at the end of the string.
The recursion (?1)
is the key. A palindrome is an empty string, a 1-character string, or a string whose first and last characters are the same and the substring between them is also a palindrome.