简体   繁体   中英

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. Do i need to modify it to make it compatible or will it run as it is.

How to use PHP regular expresion. 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.

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

PHP regexp are based on PCRE (Perl Compatible Regular Expression). Example (find digits) :

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

See php documentation .

Javascript regexp are slightly different (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 . 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);

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 .

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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