I need to validate user input that might be a regular expression for filtering fields in his/her own application, how can I check the validity of that regex?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Pass it into a dummy call to preg_match()
, and see if it returns false (indicating an error)?
Note:: Depending on where you intend this application to be run, I would question the decision to allow users to do this. You could be setting yourself up for a denial-of-service attack; all a malicious user needs to do is provide an incredibly long, complicated regex string, and your server will grind to a halt.