Pass it to the dummy call preg_match()
and see if it returns false (indicating an error)?
Note :: Depending on where you plan to run this application, I would question the decision to allow users to do this. You can set yourself up for a denial of service attack; all malicious users need to provide an incredibly long complex string of regular expressions, and your server will stop.
source share