For what it's worth, though, it is possible because most scripting languages make regular expressions more powerful by allowing you to match against previously captured values. As formally defined, regular expressions are not powerful enough to check whether an arbitrary number is prime.
Brute force but works. The real trick, for me at least, was to convert the number into a sequence of 1's. The rest was like an exercise in programming Turing machines (and a nice flashback into the freshman year of college).
For what it's worth, though, it is possible because most scripting languages make regular expressions more powerful by allowing you to match against previously captured values. As formally defined, regular expressions are not powerful enough to check whether an arbitrary number is prime.