Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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})$/);
like image 456
Shantanu Gupta Avatar asked Apr 12 '10 11:04

Shantanu Gupta


4 Answers

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.

like image 91
DuoSRX Avatar answered Sep 30 '22 03:09

DuoSRX


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

like image 26
YOU Avatar answered Sep 30 '22 02:09

YOU


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="[email protected]";

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!

like image 24
Bogdan Constantinescu Avatar answered Sep 30 '22 02:09

Bogdan Constantinescu


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.

like image 36
codaddict Avatar answered Sep 30 '22 03:09

codaddict