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