Is the php regular expression different from the js regular expression?

Source: Internet
Author: User
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, 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:
 "; $ Str = preg_replace ($ mypreg, $ mycon, $ str); echo $ str ."
";


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


Reply to discussion (solution)

$ Str = "fasdgasgdfgduuuuu1221uuuufasdfsdfsdaf4444 ";
$ Mypreg = "/(\ d {4})/I ";
$ Mycon = "this is number ";
$ Str = preg_replace ($ mypreg, $ mycon, $ str );
Echo $ str ."
";
// Fasdgasgdfgduuuuuuthis is numberuuuufasdfsdfsdafthis is number

I think the main problem is that php cannot use reverse capture.

Or php reverse capture is not like \ 1 \ 2

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"

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:
 "; $ 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 ."
";

Whether it is double quotation marks or single quotation marks, if you want to use a single \ character, it is best to use \ to represent it, so there will be no ambiguity, although single quotation marks only detect \ 'escape

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.