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