【问题标题】:js searching for pattern doesnt workjs搜索模式不起作用
【发布时间】:2015-07-14 12:53:38
【问题描述】:

我使用了一种模式来确保如果在 php 代码中运行的 char 比 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”

有什么想法吗?

【问题讨论】:

    标签: javascript string for-loop


    【解决方案1】:

    您已经转义了模式中的\。所以它试图匹配任何后跟两个\ 字符的字母。尝试将您的模式更新为/(.)\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');
    }
    

    【讨论】:

    • 感谢您的回复,但此模式在 preg_match 的 php 中有效。为什么不通过 .test 在 js 中工作? ...我想要参考可以帮助我更多地了解模式,因为我真的想更多地了解它。非常感谢
    • 因为它们是不同的实现。你为什么惊讶?从这里开始了解更多信息:developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…
    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2018-03-06
    相关资源
    最近更新 更多