squares regex: Apply optimization to factoring version

This allows a regex engine that recognizes the pattern of a backref followed by an end-anchor to optimize away the backtracking.