Q:

What is the difference between ^(?!(xx+)\1+$) and ^(?!(xx+)\1+)$?

What is the difference between ^(?!(xx+)\1+$) and ^(?!(xx+)\1+)$? Why does the former solve the "match a prime number of x" problem while the latter matches none?

A:

^(?!(xx+)\1+)$ will not match anything but the emptry string asthere is nothing to match between anchors ^ and $ (An empty string will satisfy negative lookahead).
^(?!(xx+)\1+$) can be rewritten as ^(?!(x{2,})\1+$) and will match only prime number of x. As it is negating all matches with 2+ matches of x - repeated 1 or more times.
RegEx Demo

