As for your questions about regex, they are fine.

As Terisa said, the "^$" are beginning and ending anchors.
Certain characters like [] are reserved in regex and need to be escaped, but
only in the pattern match, not the replacement text. Because we cannot replace with a pattern or character set.