Php regular expressions are different from js regular expressions.

Source: Internet
Author: User
Tags php regular expression
Is the php regular expression different from the js regular expression? I use js regular expressions. I can't match them if I write the same php regular expressions. are the regular expressions of the two different? For example, if I match four consecutive and all the same numbers, the same string, the same rule, js can match successfully, php cannot match, js: var & nbsp; str = "is the regular expression of fas php different from that of js?
I use js regular expressions. I can't match them if I write the same php regular expressions. are the regular expressions of the two different?


For example, if I match four consecutive and all the same numbers, the same string, the same rule, js can match successfully, and php cannot match,

Js:
Var str = "fasdgasgdfgduuuuu1221uuuufasdfsdfsdaf4444 ";
Var myreg =/(\ d) \ 1 {3}/gi;
Var mycon = "number ";
Var str = str. replace (myreg, mycon );
Document. write (str );


Php:
 Header ("Content-type: text/html; charset = utf-8 ");
$ Str = "fasdgasgdfgduuuuu1221uuuufasdfsdfsdaf4444 ";
$ Mypreg = "/(\ d) \ 1 {3}/I ";
$ Mycon = "this is a number ";
Echo $ str ."
";
$ Str = preg_replace ($ mypreg, $ mycon, $ str );
Echo $ str ."
";


Js successfully matches 4444, but php does not get it. Shocked !!!!!!!!!! Share:
------ Solution --------------------
The meaning of double quotation marks has taken effect!

$a = "/(\d)\1{3}/i";
$b = '/(\d)\1{3}/i';
var_dump($a == $b);
var_dump($a, $b);
Bool (false) string (11) "/(\ d) {3}/I" string (12) "/(\ d) \ 1 {3}/I"
------ Solution --------------------
Reference:
I use js regular expressions. I can't match them if I write the same php regular expressions. are the regular expressions of the two different?


For example, if I match four consecutive and all the same numbers, the same string, the same rule, js can match successfully, and php cannot match,

Js:
Var str = "fasdgasgdfgduuuuu1221uuuufasdfsdfsdaf4444 ";
Var myreg =/(\ d) \ 1 {3}/gi;
Var mycon = "number ";
Var str = str. replace (myreg, mycon );
Document. write (str );


Php:
   Header ("Content-type: text/html; charset = utf-8 ");
$ Str = "fasdgasgdfgduuuuu1221uuuufasdfsdfsdaf4444 ";
$ Mypreg = "/(\ d) \ 1 {3}/I ";
$ Mycon = "this is a number ";
Echo $ str ."
";
$ Str = preg_replace ($ mypreg, $ mycon, $ str );
Echo $ str ."
";


Js successfully matches 4444, but php does not get it. Shocked !!!!!!!!!!


The regular expression is correct. it is mainly because you use double quotation marks in PHP, causing the \ symbol transfer to take effect.

So you only need to use the single quotation mark, that is:

$ Str = "fasdgasgdfgduuuuu1221uuuufasdfsdfsdaf4444 ";
$ Mypreg = '/(\ d) \ 1 {3}/I ';
$ Mycon = "this is a number ";
Echo $ str ."
";
$ Str = preg_replace ($ mypreg, $ mycon, $ str );
Echo $ str ."
";

------ Solution --------------------
We recommend that you use a single

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.