我使用js的正则没有问题,然后同样的写成php正则,居然匹配不到,两者的正则不一样?
如,我匹配4个连续且全部相同的数字,同样的串,同样的规则,js可以匹配成功,php匹配不到,
js:
var str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";var myreg=/(\d)\1{3}/gi;var mycon="数字";var str=str.replace(myreg,mycon);document.write(str);
<?phpheader("Content-type:text/html;charset=utf-8");$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";$mypreg="/(\d)\1{3}/i";$mycon="这是数字";echo $str."<br/>"; $str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";
$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";
$mypreg="/(\d{4})/i";
$mycon="this is number";
$str=preg_replace($mypreg,$mycon,$str);
echo $str."076402276aae5dbec7f672f8f4e5cc81";
//fasdgasgdfgfduuuuuuthis is numberuuuuufasdfsdfsdafthis is number
我觉得主要问题是php无法使用反向捕获的原因。
或者php的反向捕获不是\1 \2这样的
双引号中转义生效了!
$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"
我使用js的正则没有问题,然后同样的写成php正则,居然匹配不到,两者的正则不一样?
如,我匹配4个连续且全部相同的数字,同样的串,同样的规则,js可以匹配成功,php匹配不到,
js:
var str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";var myreg=/(\d)\1{3}/gi;var mycon="数字";var str=str.replace(myreg,mycon);document.write(str);
<?phpheader("Content-type:text/html;charset=utf-8");$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";$mypreg="/(\d)\1{3}/i";$mycon="这是数字";echo $str."<br/>"; $str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";
$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";$mypreg='/(\d)\1{3}/i';$mycon="这是数字";echo $str."<br/>"; $str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";
建议无论是不是双引号和单引号之中,如果想用单个 \字符,最好都是用 \\来表示,这样是不会出现歧义的,尽管单引号仅检测 \'转义