问题描述
我有一个允许用户输入正则表达式的应用程序.我该如何检查正则表达式的任何输入并确保它们是有效的,因为如果不存在它们,则会出现preg_match错误?
I have an application that enables the user to input a regular expression. How do I check against any input of regular expressions and make sure they are valid ones, because if they are not there there will be preg_match errors?
我不想在preg_match之前使用'@',所以如果有一种方法可以检查正则表达式的用户输入的有效性,那么就很好.
I don't want to use the '@' before preg_match, so if there's a way to check the validity of the user input of regular expressions that'd be great.
PHP的正则表达式系统似乎过于复杂,以至于我无法为其提供正则表达式.
The regular expression system of PHP seems to be rather too complicated for me to come up with a regular expression for them.
推荐答案
preg_match()
返回FALSE
.
- 将表达式发送到服务器
-
preg_match
在空字符串上 - 查看是否发生错误
- send the expression to the server
preg_match
on an empty string- see if an error occurs
您可以使用Ajax进行实时验证,也可以在提交表单后进行验证.
您也可以尝试通过将表达式提供给javascript regexp引擎来进行验证,但js regexp语法与php并非100%兼容.
You can either use Ajax to validate real time, or validate after form submit.
You can also try to validate by feeding the expression to javascript regexp engine, but js regexp syntax is not 100% compatible with the php one.
这篇关于正则表达式的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!