简体   繁体   English

我可以在php中使用javascript正则表达式吗?

[英]Can I use javascript regular expression in php as it is

I am using a regular expression in javascript and want to do server side validation as well with the same regular expression. 我在javascript中使用正则表达式,并希望使用相同的正则表达式进行服务器端验证。 Do i need to modify it to make it compatible or will it run as it is. 我需要修改它以使其兼容,还是按原样运行。

How to use PHP regular expresion. 如何使用PHP正则表达式。 Please provide a small example. 请提供一个小例子。

Thanks in Advance 提前致谢

EDIT 编辑

For Email Validation 用于电子邮件验证

var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

For Phone no validation 对于电话没有验证

var pattern = new RegExp(/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/);

Supposed to work for most of the patterns, except escaping special characters and backslashes, but not reverse, php regex have features than javascript like look behind expressions. 假设适用于大多数模式,除了转义特殊字符和反斜杠,但不反向,php正则表达式具有javascript之类的功能,就像看后面的表达式一样。

javascript : /[a-z]+/
php        : '/[a-z]+/'

For example, 例如,

var pattern = new RegExp(/^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$/);

would be '/^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$/' in php 将是'/^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$/'在php中

PHP regexp are based on PCRE (Perl Compatible Regular Expression). PHP regexp基于PCRE(Perl兼容正则表达式)。 Example (find digits) : 示例(查找数字):

preg_match('/^[0-9]*$/', 'my01string');

See php documentation . 请参阅php文档

Javascript regexp are slightly different (ECMA). Javascript regexp略有不同(ECMA)。

var patt1 = new RegExp("e");
document.write(patt1.test("The best things in life are free"));

See here for a comparison table. 请参见此处的比较表。

You should use one of the following functions preg_match or preg_match_all . 您应该使用以下函数之一preg_matchpreg_match_all And with a bit of luck you shouldn't need to modify your regex. 运气好的话,你不需要修改你的正则表达式。 PHP regex uses the classic Perl regex, so a match would look like preg_match_all('/([a-zA-Z0-9]+)/', $myStringToBeTested, $results); PHP正则表达式使用经典的Perl正则表达式,因此匹配看起来像preg_match_all('/([a-zA-Z0-9]+)/', $myStringToBeTested, $results);

Later edit: 稍后编辑:

$string="test@mail.com";

if(preg_match('/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/', $string))
    echo "matches!";
else
    echo "doesn't match!";

Enjoy! 请享用!

In PHP we use the function preg_match . 在PHP中,我们使用函数preg_match

$email_pattern = '/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i';

$phoneno_pattern = '^/\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/';

if(preg_match($email_pattern,$input_email)) {
 // valid email.
}

if(preg_match($phoneno_pattern,$input_ph)) {
 // valid ph num.
}

You could have used the regex directly as the function argument instead of using a variable. 您可以直接使用正则表达式作为函数参数,而不是使用变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM