Javascript js搜索模式不起作用

Javascript js搜索模式不起作用,javascript,string,for-loop,Javascript,String,For Loop,我使用了一个模式来确保php代码中是否有与3个字符相同的字符出现并正常工作,但当我在javascript中尝试它时,它没有任何效果 if (!(/(.)\\1{2}/.test(string))) { console.log('there are no running 3 chars occur in the string'); } else{ console.log('there are 3 running same char occur in the string'); } 控制台

我使用了一个模式来确保php代码中是否有与3个字符相同的字符出现并正常工作,但当我在javascript中尝试它时,它没有任何效果

if (!(/(.)\\1{2}/.test(string))) {
  console.log('there are no running 3 chars occur in the string');
} else{
  console.log('there are 3 running same char occur in the string');
}
控制台给我这里没有运行3字符出现在字符串中,虽然字符串是iii

有什么想法吗?

您已经逃脱了模式中的\。因此,它试图匹配后跟两个\字符的任何字母。尝试将模式更新为/\1{2}/

if (!(/(.)\\1{2}/.test(string))) {
  console.log('there are no running 3 chars occur in the string');
} else{
  console.log('there are 3 running same char occur in the string');
}

感谢您的回复,但是这个模式在php中通过preg_match工作。为什么不在js中工作。测试任何想法。。。我想参考,可以帮助我更多的模式,因为我真的想了解更多。非常感谢,因为它们是不同的实现。你为什么感到惊讶?从这里开始了解更多信息: